Skip to content

Commit

Permalink
Fixing unset command on multiple runs; Fixing stack list indicator; A…
Browse files Browse the repository at this point in the history
…dding changie;
  • Loading branch information
Shackelford-Arden committed Feb 29, 2024
1 parent e2fab95 commit 5bdbf86
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 66 deletions.
4 changes: 4 additions & 0 deletions .changes/0.3.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 0.3.1 - 2024-02-28
### Fixed
* Bug where listing stacks when no stack is currently selected adds an inidicator to every stack
* unset command erroring out if no stack is active
6 changes: 6 additions & 0 deletions .changes/header.tpl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
and is generated by [Changie](https://github.com/miniscruff/changie).
Empty file added .changes/unreleased/.gitkeep
Empty file.
31 changes: 31 additions & 0 deletions .changie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
changesDir: .changes
unreleasedDir: unreleased
headerPath: header.tpl.md
changelogPath: CHANGELOG.md
versionExt: md
versionFormat: '## {{.Version}} - {{.Time.Format "2006-01-02"}}'
kindFormat: '### {{.Kind}}'
changeFormat: '* {{.Body}}'
kinds:
- label: Added
auto: minor
- label: Changed
auto: major
- label: Deprecated
auto: minor
- label: Removed
auto: major
- label: Fixed
auto: patch
- label: Security
auto: patch
newlines:
afterChangelogHeader: 1
beforeChangelogVersion: 1
endOfVersion: 1
envPrefix: CHANGIE_

custom:
- key: Author
label: Github Name
type: string
1 change: 1 addition & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ archives:
format: zip

changelog:
disable: false
sort: asc
filters:
exclude:
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
and is generated by [Changie](https://github.com/miniscruff/changie).


No releases yet, this file will be updated when generating your first release.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ _Note: hctx will only unset the environment variables that are configured in the
hctx unset
```

Note: There is currently a bug where if you run the `unset` command more than once in a row, it errors out.

### Shell Prompts

This section contains information about how one _might_ configure the
Expand Down
7 changes: 4 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/Shackelford-Arden/hctx/types"
"os"

"github.com/Shackelford-Arden/hctx/models"
Expand All @@ -11,7 +12,7 @@ import (
func List(ctx *cli.Context) error {

// Parse config
cfg, cfgErr := models.NewConfig()
cfg, cfgErr := models.NewConfig("")
if cfgErr != nil {
return cfgErr
}
Expand All @@ -21,12 +22,12 @@ func List(ctx *cli.Context) error {
return nil
}

currStack := os.Getenv("HCTX_STACK_NAME")
currStack := os.Getenv(types.StackNameEnv)

fmt.Println("Stacks:")
for _, stack := range cfg.Stacks {
var indicator string
if stack.Name == currStack || stack.Alias == currStack {
if currStack != "" && (stack.Name == currStack || stack.Alias == currStack) {
indicator = "*"
}
fmt.Printf(" %s %s\n", stack.Name, indicator)
Expand Down
6 changes: 3 additions & 3 deletions cmd/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ package cmd
import (
"fmt"
"github.com/Shackelford-Arden/hctx/models"
"github.com/Shackelford-Arden/hctx/types"
"github.com/urfave/cli/v2"
"os"
)

// Unset Remove everything hctx configured in the environment variables
func Unset(ctx *cli.Context) error {

currStack := os.Getenv("HCTX_STACK_NAME")
currStack := os.Getenv(types.StackNameEnv)

if currStack == "" {
fmt.Println("No stack selected, nothing to unset.")
return nil
}

// Parse config
cfg, cfgErr := models.NewConfig()
cfg, cfgErr := models.NewConfig("")
if cfgErr != nil {
return cfgErr
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Use(ctx *cli.Context) error {
var selectedStack *models.Stack

// Parse config
cfg, cfgErr := models.NewConfig()
cfg, cfgErr := models.NewConfig("")
if cfgErr != nil {
return cfgErr
}
Expand Down
46 changes: 26 additions & 20 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"fmt"
"github.com/Shackelford-Arden/hctx/types"
"github.com/hashicorp/hcl/v2/hclsimple"
"os"
"strings"
Expand All @@ -20,15 +21,20 @@ type Stack struct {
Vault *VaultConfig `hcl:"vault,block"`
}

func NewConfig() (*Config, error) {
// Get user homedir
userHome, homeErr := os.UserHomeDir()
if homeErr != nil {
fmt.Printf("failed to get user homedir: %s", homeErr)
os.Exit(10)
}
func NewConfig(cp string) (*Config, error) {

var configPath = cp

configPath := fmt.Sprintf("%s/.config/%s", userHome, ".hctx.hcl")
if cp == "" {
// Get user homedir
userHome, homeErr := os.UserHomeDir()
if homeErr != nil {
fmt.Printf("failed to get user homedir: %s", homeErr)
os.Exit(10)
}

configPath = fmt.Sprintf("%s/.config/%s", userHome, ".hctx.hcl")
}

// Check if there is a config file
_, statErr := os.Stat(configPath)
Expand Down Expand Up @@ -60,51 +66,51 @@ func NewConfig() (*Config, error) {

// Use provides commands to set appropriate environment variables
func (s *Stack) Use(shell string) string {
var exportCommand string

// Include Stack Name as an environment variable
// Allow the Alias name to show in the environment variable
stackName := s.Name
if s.Alias != "" {
stackName = s.Alias
}
exportCommand += strings.Join([]string{fmt.Sprintf("export HCTX_STACK_NAME='%s' \n", stackName)}, exportCommand)
var exportCommands = []string{fmt.Sprintf("\nexport %s='%s'", types.StackNameEnv, stackName)}

if s.Nomad != nil {
exportCommand += strings.Join(s.Nomad.Use(shell), exportCommand)
exportCommands = append(exportCommands, s.Nomad.Use(shell)...)
}

if s.Consul != nil {
exportCommand += strings.Join(s.Consul.Use(shell), exportCommand)
exportCommands = append(exportCommands, s.Consul.Use(shell)...)
}

if s.Vault != nil {
exportCommand += strings.Join(s.Vault.Use(shell), exportCommand)
exportCommands = append(exportCommands, s.Vault.Use(shell)...)
}

var exportCommand = strings.Join(exportCommands, "\n")

return exportCommand
}

// Unset Provides shell commands to unset environment variables
func (s *Stack) Unset(shell string) string {

var unsetCommand string

// Remove Stack environment variables
unsetCommand += strings.Join([]string{"unset HCTX_STACK_NAME\n"}, unsetCommand)
var unsetCommands = []string{fmt.Sprintf("\nunset %s", types.StackNameEnv)}

if s.Nomad != nil {
unsetCommand += strings.Join(s.Nomad.Unset(shell), unsetCommand)
unsetCommands = append(unsetCommands, s.Nomad.Unset(shell)...)
}

if s.Consul != nil {
unsetCommand += strings.Join(s.Consul.Unset(shell), unsetCommand)
unsetCommands = append(unsetCommands, s.Consul.Unset(shell)...)
}

if s.Vault != nil {
unsetCommand += strings.Join(s.Vault.Unset(shell), unsetCommand)
unsetCommands = append(unsetCommands, s.Vault.Unset(shell)...)
}

var unsetCommand = strings.Join(unsetCommands, "\n")

return unsetCommand
}

Expand Down
97 changes: 97 additions & 0 deletions models/config_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,98 @@
package models

import (
"fmt"
"github.com/Shackelford-Arden/hctx/types"
"testing"
)

// region Bash/ZSH

func TestNewConfigUseFullNomad(t *testing.T) {

cfg, err := NewConfig("testdata/fullNomad.hcl")
if err != nil {
t.Fatalf("failed to read config: %s", err)
}

stackName := "just_nomad"
useOut := cfg.StackExists(stackName).Use("bash")

expectedOutput := fmt.Sprintf(`
export %s='%s'
export %s=http://localhost:4646
export %s=default`,
types.StackNameEnv, stackName, NomadAddr, NomadNamespace)

if useOut != expectedOutput {
t.Fatalf("\nExpected: %s\nActual: %s", expectedOutput, useOut)
}

}

func TestNewConfigUseFullConsul(t *testing.T) {

cfg, err := NewConfig("testdata/fullConsul.hcl")
if err != nil {
t.Fatalf("failed to read config: %s", err)
}
stackName := "just_consul"
useOut := cfg.StackExists(stackName).Use("bash")

expectedOutput := fmt.Sprintf(`
export %s='%s'
export %s=http://localhost:8500
export %s=default`,
types.StackNameEnv, stackName, ConsulAddr, ConsulNamespace)

if useOut != expectedOutput {
t.Fatalf("\nExpected: %s\nActual: %s", expectedOutput, useOut)
}

}

func TestNewConfigUseFullVault(t *testing.T) {

cfg, err := NewConfig("testdata/fullVault.hcl")
if err != nil {
t.Fatalf("failed to read config: %s", err)
}

stackName := "just_vault"
useOut := cfg.StackExists(stackName).Use("bash")

expectedOutput := fmt.Sprintf(`
export %s='%s'
export %s=http://localhost:8200
export %s=default`,
types.StackNameEnv, stackName, VaultAddr, VaultNamespace)

if useOut != expectedOutput {
t.Fatalf("\nExpected: %s\nActual: %s", expectedOutput, useOut)
}

}

func TestNewConfigUnsetNomad(t *testing.T) {

cfg, err := NewConfig("testdata/fullNomad.hcl")
if err != nil {
t.Errorf("failed to read config: %s", err)
}

stackName := "just_nomad"
unsetOut := cfg.StackExists(stackName).Unset("bash")

expectedOutput := fmt.Sprintf(`
unset %s
unset %s
unset %s`,
types.StackNameEnv, NomadAddr, NomadNamespace)

if unsetOut != expectedOutput {
t.Fatalf("\nExpected: %s\nActual: %s", expectedOutput, unsetOut)
}

}

// endregion
31 changes: 0 additions & 31 deletions models/nomad_test.go

This file was deleted.

6 changes: 6 additions & 0 deletions models/testdata/fullConsul.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
stack "just_consul" {
consul {
address = "http://localhost:8500"
namespace = "default"
}
}
6 changes: 6 additions & 0 deletions models/testdata/fullNomad.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
stack "just_nomad" {
nomad {
address = "http://localhost:4646"
namespace = "default"
}
}
6 changes: 6 additions & 0 deletions models/testdata/fullVault.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
stack "just_vault" {
vault {
address = "http://localhost:8200"
namespace = "default"
}
}
Loading

0 comments on commit 5bdbf86

Please sign in to comment.