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

[WIP] Feature: Add support for displaying the active Pulumi stack #357

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var modules = map[string]func(*powerline) []pwl.Segment{
"perlbrew": segmentPerlbrew,
"plenv": segmentPlEnv,
"perms": segmentPerms,
"pulumi": segmentPulumiStack,
"rbenv": segmentRbenv,
"root": segmentRoot,
"rvm": segmentRvm,
Expand Down
74 changes: 74 additions & 0 deletions segment-pulumi_stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"

pwl "github.com/justjanne/powerline-go/powerline"
"gopkg.in/yaml.v2"
)

const ppFile = "Pulumi.yaml"

type Project struct {
Name string
Runtime string
Description string
}

type Workspace struct {
Stack string
}

func segmentPulumiStack(p *powerline) []pwl.Segment {
// Check for presence of Pulumi.yaml in current directory
projDir, err := os.Getwd()
projFile := filepath.Join(projDir, ppFile)
stat, err := os.Stat(projFile)
if err != nil {
return []pwl.Segment{}
}
if stat.IsDir() {
return []pwl.Segment{}
}

// Get active stack from project workspace JSON file
pulumiHome := os.Getenv("PULUMI_HOME")
if pulumiHome == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return []pwl.Segment{}
}
pulumiHome = homeDir + "/.pulumi"
}
y, err := ioutil.ReadFile(projFile)
if err != nil {
return []pwl.Segment{}
}
var projectInfo Project
err = yaml.Unmarshal(y, &projectInfo)
projName := projectInfo.Name
hash := sha1.New()
hash.Write([]byte(projFile))
sha1_hash := hex.EncodeToString(hash.Sum(nil))
wsFilename := pulumiHome + "/workspaces/" + projName + "-" + sha1_hash + "-workspace.json"
j, err := ioutil.ReadFile(wsFilename)
if err != nil {
return []pwl.Segment{}
}
var currentWs Workspace
err = json.Unmarshal(j, &currentWs)
activeStack := currentWs.Stack

return []pwl.Segment{{
Name: "pulumi-stack",
Content: strings.Replace(string(activeStack), "\n", "", -1),
Foreground: p.theme.TFWsFg,
Background: p.theme.TFWsBg,
}}
}