Aside from running `pulumi stack ls` (or making an...
# general
s
Aside from running
pulumi stack ls
(or making an equivalent API call), is there any way to determine the active stack for a Pulumi project? I'm not seeing anything in the stack-specific YAML files that would denote active or not.
e
There is but I wouldn't recommend using it. Every project directory makes a file in ~/.pulumi/workspaces which looks like "<project_name>-<hash_of_path>-workspace.json" and that records the active stack.
Relevant code:
Copy code
func (pw *projectWorkspace) settingsPath() string {
	uniqueFileName := string(pw.name) + "-" + sha1HexString(pw.project) + "-" + WorkspaceFile
	path, err := GetPulumiPath(WorkspaceDir, uniqueFileName)
	contract.AssertNoErrorf(err, "could not get workspace path")
	return path
}

// sha1HexString returns a hex string of the sha1 hash of value.
func sha1HexString(value string) string {
	// nolint: gosec
	h := sha1.New()
	_, err := h.Write([]byte(value))
	contract.AssertNoError(err)
	return hex.EncodeToString(h.Sum(nil))
}
So you could get the sha1 hex string of the Pulumi.yaml file then look it up there, but A) That's horrible just use
pulumi ls
B) This is not a supported interface and we'll probably move those files to somewhere more sensible at some point But that's the state of things now if the risk tradeoff is worth it to you.
s
Hmm...OK, thanks. I was asking because I was trying to incorporate support for displaying the active Pulumi stack in the prompt using
powerline-go
(https://github.com/justjanne/powerline-go). Doing something local would generally result in lower latency (useful for a prompt) than having to call out to the Pulumi service.
e
Oh if your in the project directory
pulumi stack --show-name
That should be quite fast
s
I'll give that a look. Does that make a call out to the service in order to return the value?
e
No its just checking the local files
👍🏻 1
c
This is what Starship (another popular PS1 written in Rust) does https://github.com/starship/starship/blob/master/src/modules/pulumi.rs#L116
e
N.B. Aaron works at Pulumi 🙂 And
pulumi stack --show-name
was recently changed to be much faster (hopefully fast enough for a PS1 prompt) I'll nudge Aaron to see about changing starship to try using it as well.
c
Oh cool I didn’t know that!
Oh it looks like I linked his fork instead of the upstream
e
s
I don’t know Rust, but it looks like it’s parsing the JSON files in
~/.pulumi/workspaces
. I have a fork of
powerline-go
working using
pulumi stack --show-name
, but there is a noticeable delay in the prompt. I’ll have to try parsing the JSON files and see if that works better.