https://pulumi.com logo
#general
Title
# general
s

salmon-account-74572

05/04/2022, 8:19 PM
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

echoing-dinner-19531

05/04/2022, 8:32 PM
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

salmon-account-74572

05/04/2022, 10:05 PM
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

echoing-dinner-19531

05/04/2022, 10:13 PM
Oh if your in the project directory
pulumi stack --show-name
That should be quite fast
s

salmon-account-74572

05/04/2022, 10:17 PM
I'll give that a look. Does that make a call out to the service in order to return the value?
e

echoing-dinner-19531

05/04/2022, 10:34 PM
No its just checking the local files
👍🏻 1
c

calm-jackal-58777

05/05/2022, 10:08 AM
This is what Starship (another popular PS1 written in Rust) does https://github.com/starship/starship/blob/master/src/modules/pulumi.rs#L116
e

echoing-dinner-19531

05/05/2022, 10:13 AM
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

calm-jackal-58777

05/05/2022, 10:55 AM
Oh cool I didn’t know that!
Oh it looks like I linked his fork instead of the upstream
e

echoing-dinner-19531

05/05/2022, 11:25 AM
s

salmon-account-74572

05/05/2022, 2:27 PM
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.
14 Views