Skip to content

Commit

Permalink
add windows support for file creation. Add more descriptive cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
ybelMekk committed Sep 17, 2021
1 parent 48ec1a5 commit 0be6a07
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
6 changes: 4 additions & 2 deletions cmd/aiven_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ const (

var aivenCommand = &cobra.Command{
Use: "aiven [command] [args] [flags]",
Short: "Create a aivenApplication to your cluster",
Long: `This command will apply a aivenApplication based on information given and avienator will create a set of credentials`,
Short: "Create a protected & time-limited aivenApplication",
Long: `This command will apply a aivenApplication based on information given and aivenator creates a set of credentials`,
Example: `nais-cli aiven username namespace | nais-cli aiven username namespace -p nav-dev |
nais-cli aiven username namespace -e 10 | nais-cli aiven username namespace -s some-secret-name`,
RunE: func(cmd *cobra.Command, args []string) error {

if len(args) != 2 {
Expand Down
20 changes: 9 additions & 11 deletions cmd/get_cmd.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
package cmd

import (
"fmt"
"github.com/nais/nais-cli/cmd/helpers"
"github.com/nais/nais-cli/pkg/config"
"github.com/nais/nais-cli/pkg/secret"
"github.com/spf13/cobra"
"os"
"log"
)

var getCmd = &cobra.Command{
Use: "get [args] [flags]",
Short: "Returns the specified config format from a protected secret and generates credentials to 'current' location",
Short: "Return the preferred config format from a protected secret and generate files to location",
Example: `nais-cli aiven get secret-name namespace | nais-cli aiven get secret-name namespace -d ./config |
nais-cli aiven get secret-name namespace -c kcat | nais-cli aiven get secret-name namespace -c .env |
nais-cli aiven get secret-name namespace -c all`,
Run: func(cmd *cobra.Command, args []string) {

if len(args) != 2 {
fmt.Printf("%s and %s is reqired arguments", SecretNameFlag, TeamFlag)
os.Exit(1)
log.Fatalf("%s and %s is reqired arguments", SecretNameFlag, TeamFlag)
}

secretName := args[0]
team := args[1]

configType, err := helpers.GetString(cmd, ConfigFlag, false)
if err != nil {
fmt.Printf("getting %s: %s", ConfigFlag, err)
os.Exit(1)
log.Fatalf("getting %s: %s", ConfigFlag, err)
}

if configType != config.ENV && configType != config.ALL && configType != config.KCAT {
fmt.Printf("valid args: %s | %s | %s", config.ENV, config.KCAT, config.ALL)
os.Exit(1)
log.Fatalf("valid args: %s | %s | %s", config.ENV, config.KCAT, config.ALL)
}

dest, err := helpers.GetString(cmd, DestFlag, false)
if err != nil {
fmt.Printf("getting %s: %s", DestFlag, err)
os.Exit(1)
log.Fatalf("getting %s: %s", DestFlag, err)
}
secret.ExtractAndGenerateConfig(configType, dest, secretName, team)
},
Expand Down
26 changes: 19 additions & 7 deletions cmd/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"runtime"
"strings"
)

Expand All @@ -13,20 +14,17 @@ const (
)

func DefaultDestination(dest string) (string, error) {
path, err := os.Getwd()
current, err := os.Getwd()
if dest == "" {
return path, nil
return current, nil
}

if !strings.HasPrefix(dest, "/") {
dest = fmt.Sprintf("/%s", dest)
}

newPath := fmt.Sprintf("%s%s", path, dest)
if err != nil {
return "", fmt.Errorf("could assign directory; %s", err)
}

dest = system(dest)
newPath := fmt.Sprintf("%s%s", current, dest)
if _, err := os.Stat(newPath); os.IsNotExist(err) {
if err = os.Mkdir(newPath, os.FileMode(FilePermission)); err != nil {
return "", fmt.Errorf("could not create directory; %s", err)
Expand All @@ -35,6 +33,20 @@ func DefaultDestination(dest string) (string, error) {
return newPath, nil
}

func system(dest string) string {
if runtime.GOOS == "windows" {
if !strings.HasPrefix(dest, "\\") {
return fmt.Sprintf("\\%s", dest)
} else {
return dest
}
}
if !strings.HasPrefix(dest, "/") {
return fmt.Sprintf("/%s", dest)
}
return dest
}

func GetString(cmd *cobra.Command, flag string, required bool) (string, error) {
if viper.GetString(flag) != "" {
return viper.GetString(flag), nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/root_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var (

rootCmd = &cobra.Command{
Use: "nais-cli [command] [args] [flags]",
Short: "A simple nais client to generate resources for debug",
Short: "A simple NAIS client to generate resources for debug",
Long: `nais-cli debug CLI.
This is a nais tool to extract secrets from cluster to quickly start debugging your nais resources.`,
This is a NAIS tool to extract secrets from cluster to quickly start debugging your NAIS resources.`,
}
)

Expand Down

0 comments on commit 0be6a07

Please sign in to comment.