Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stdinIsATTY template function #1111

Merged
merged 1 commit into from
Mar 29, 2021
Merged

Add stdinIsATTY template function #1111

merged 1 commit into from
Mar 29, 2021

Conversation

twpayne
Copy link
Owner

@twpayne twpayne commented Mar 28, 2021

Fixes #1109.

@felipecrs would you be able to test this? I'm not sure how it behaves on Windows.

@felipecrs
Copy link
Contributor

Sure, let me try.

@twpayne
Copy link
Owner Author

twpayne commented Mar 28, 2021

Ah, you need the --init flag to chezmoi execute-template because stdinIsATTY is only available for generating the initial config file.

@felipecrs
Copy link
Contributor

I realized it, sorry. I'm trying to figure out what to do to not have a stdin, to check.

@felipecrs
Copy link
Contributor

felipecrs commented Mar 28, 2021

Well, it doesn't work in the following scenario:

$ docker run -v c:\Users\felip\Downloads:c:\Downloads mcr.microsoft.com/windows:20H2 C:\Downloads\chezmoi.exe execute-template --init '{{ stdinIsATTY }}'
true

@felipecrs
Copy link
Contributor

felipecrs commented Mar 28, 2021

But surprisingly, it works like a charm when using a chezmoi init. See:

> cat .\.chezmoi.toml.tmpl
{{ $email := "" }}
{{ if stdinIsATTY }}
{{   $email = promptString "email" }}
{{ else }}
{{   $email = "[email protected]" }}
{{ end }}

[data]
    email = "{{ $email }}"
> docker run -v c:\Users\felip\Downloads:c:\Downloads mcr.microsoft.com/windows:20H2 powershell -command 'C:\Downloads\chezmoi.exe init --verbose --source C:\Downloads; C:\Downloads\chezmoi.exe data --verbose --source C:\Downloads'
{
  "chezmoi": {
    "arch": "amd64",
    "fqdnHostname": "7d2ad3306736",
    "homeDir": "C:\\Users\\ContainerAdministrator",
    "homedir": "C:\\Users\\ContainerAdministrator",
    "hostname": "7d2ad3306736",
    "kernel": null,
    "os": "windows",
    "osRelease": {},
    "sourceDir": "C:/Downloads",
    "username": "User Manager\\ContainerAdministrator",
    "version": {
      "builtBy": "goreleaser",
      "commit": "56485778b5334f3aa05b5d12abff0db00cc8cb2b",
      "date": "2021-03-28T18:30:09Z",
      "version": "v2.0.4-SNAPSHOT-5648577"
    }
  },
  "email": "[email protected]"
}
> docker run -ti -v c:\Users\felip\Downloads:c:\Downloads mcr.microsoft.com/windows:20H2 powershell -command 'C:\Downloads\chezmoi.exe init --verbose --source C:\Downloads; C:\Downloads\chezmoi.exe data --verbose --source C:\Downloads'
email? teste
{
  "chezmoi": {
    "arch": "amd64",
    "fqdnHostname": "9e5d16128b04",
    "homeDir": "C:\\Users\\ContainerAdministrator",
    "homedir": "C:\\Users\\ContainerAdministrator",
    "hostname": "9e5d16128b04",
    "kernel": null,
    "os": "windows",
    "osRelease": {},
    "sourceDir": "C:/Downloads",
    "username": "User Manager\\ContainerAdministrator",
    "version": {
      "builtBy": "goreleaser",
      "commit": "56485778b5334f3aa05b5d12abff0db00cc8cb2b",
      "date": "2021-03-28T18:30:09Z",
      "version": "v2.0.4-SNAPSHOT-5648577"
    }
  },
  "email": "teste"
}

Nonetheless, when using only docker run -t, the chezmoi still thinks the stdin is interactive, and keeps waiting for an answer (when none is possible to be inputted). But I think it's fine. It should probably be a caveat of Docker Windows containers or whatever.

> docker run -t -v c:\Users\felip\Downloads:c:\Downloads mcr.microsoft.com/windows:20H2 powershell -command 'C:\Downloads\chezmoi.exe init --verbose --source C:\Downloads; C:\Downloads\chezmoi.exe data --verbose --source C:\Downloads'
email?

@felipecrs
Copy link
Contributor

If you want me to test anything, feel free to ask me. It took me a while to setup this Windows containers environment, hahaha.

@twpayne twpayne merged commit 7dd00b2 into master Mar 29, 2021
@twpayne twpayne deleted the stdinisatty branch March 29, 2021 16:09
@twpayne
Copy link
Owner Author

twpayne commented Mar 29, 2021

Thanks so much for the testing @felipecrs :)

I had no idea that Windows container environments existed. This is potentially really interesting for CI.

@felipecrs
Copy link
Contributor

Yeah... the only caveat is that you need a Windows host, and the Windows docker image is around 6GB to pull. There are smaller ones, such as the Windows Nano Server (150MB), but sadly chezmoi could not run due to missing DLLs.

@twpayne
Copy link
Owner Author

twpayne commented Mar 29, 2021

Ah, thanks. It also looks like you need Windows 10 Pro to run Windows images, and I only have Windows 10 Home on my laptop.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New template function: isatty
2 participants