Skip to content
Merged
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dist/

# Icon must end with two \r
Icon
vendor


# Thumbnails
Expand Down
37 changes: 18 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
VERSION=v0.0.7
VERSION=v0.0.8
OUT_DIR=dist

CLI_NAME?=cf
IMAGE_REPOSITORY?=quay.io
IMAGE_NAMESPACE?=codefresh

INSTALLATION_MANIFESTS_URL="github.com/codefresh-io/cli-v2/manifests?ref=$(VERSION)"
INSTALLATION_MANIFESTS_NAMESPACED_URL="github.com/codefresh-io/cli-v2/manifests/namespace-install?ref=$(VERSION)"
ARGOCD_INSTALLATION_MANIFESTS_URL="github.com/codefresh-io/cli-v2/manifests/argo-cd?ref=$(VERSION)"
EVENTS_INSTALLATION_MANIFESTS_URL="github.com/codefresh-io/cli-v2/manifests/argo-events?ref=$(VERSION)"
ROLLOUTS_INSTALLATION_MANIFESTS_URL="github.com/codefresh-io/cli-v2/manifests/argo-rollouts?ref=$(VERSION)"
WORKFLOWS_INSTALLATION_MANIFESTS_URL="github.com/codefresh-io/cli-v2/manifests/argo-workflows?ref=$(VERSION)"

DEV_INSTALLATION_MANIFESTS_URL="manifests/"
DEV_INSTALLATION_MANIFESTS_NAMESPACED_URL="manifests/namespace-install"
DEV_ARGOCD_INSTALLATION_MANIFESTS_URL="manifests/argo-cd"
DEV_EVENTS_INSTALLATION_MANIFESTS_URL="manifests/argo-events"
DEV_ROLLOUTS_INSTALLATION_MANIFESTS_URL="manifests/argo-rollouts"
DEV_WORKFLOWS_INSTALLATION_MANIFESTS_URL="manifests/argo-workflows"

CLI_SRCS := $(shell find . -name '*.go')

MKDOCS_DOCKER_IMAGE?=squidfunk/mkdocs-material:4.1.1
PACKR_CMD=$(shell if [ "`which packr`" ]; then echo "packr"; else echo "go run github.com/gobuffalo/packr/packr"; fi)

GIT_COMMIT=$(shell git rev-parse HEAD)
BUILD_DATE=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')

DEV_MODE?=true

ifeq (${DEV_MODE},true)
INSTALLATION_MANIFESTS_URL=${DEV_INSTALLATION_MANIFESTS_URL}
INSTALLATION_MANIFESTS_NAMESPACED_URL=${DEV_INSTALLATION_MANIFESTS_NAMESPACED_URL}
ARGOCD_INSTALLATION_MANIFESTS_URL=${ARGOCD_DEV_INSTALLATION_MANIFESTS_URL}
EVENTS_INSTALLATION_MANIFESTS_URL=${EVENTS_DEV_INSTALLATION_MANIFESTS_URL}
ROLLOUTS_INSTALLATION_MANIFESTS_URL=${ROLLOUTS_DEV_INSTALLATION_MANIFESTS_URL}
WORKFLOWS_INSTALLATION_MANIFESTS_URL=${WORKFLOWS_DEV_INSTALLATION_MANIFESTS_URL}
endif

ifndef GOBIN
Expand Down Expand Up @@ -81,16 +86,17 @@ $(OUT_DIR)/$(CLI_NAME)-%.sha256:
@make $(OUT_DIR)/$(CLI_NAME)-$*.tar.gz
openssl dgst -sha256 "$(OUT_DIR)/$(CLI_NAME)-$*.tar.gz" | awk '{ print $$2 }' > "$(OUT_DIR)/$(CLI_NAME)-$*".sha256

$(OUT_DIR)/$(CLI_NAME)-%: $(CLI_SRCS) $(GOBIN)/packr
$(OUT_DIR)/$(CLI_NAME)-%: $(CLI_SRCS)
@GO_FLAGS=$(GO_FLAGS) \
BUILD_DATE=$(BUILD_DATE) \
BINARY_NAME=$(CLI_NAME) \
VERSION=$(VERSION) \
GIT_COMMIT=$(GIT_COMMIT) \
PACKR_CMD=$(PACKR_CMD) \
OUT_FILE=$(OUT_DIR)/$(CLI_NAME)-$* \
INSTALLATION_MANIFESTS_URL=$(INSTALLATION_MANIFESTS_URL) \
INSTALLATION_MANIFESTS_NAMESPACED_URL=$(INSTALLATION_MANIFESTS_NAMESPACED_URL) \
ARGOCD_INSTALLATION_MANIFESTS_URL=$(ARGOCD_INSTALLATION_MANIFESTS_URL) \
EVENTS_INSTALLATION_MANIFESTS_URL=$(EVENTS_INSTALLATION_MANIFESTS_URL) \
ROLLOUTS_INSTALLATION_MANIFESTS_URL=$(ROLLOUTS_INSTALLATION_MANIFESTS_URL) \
WORKFLOWS_INSTALLATION_MANIFESTS_URL=$(WORKFLOWS_INSTALLATION_MANIFESTS_URL) \
MAIN=./cmd \
./hack/build.sh

Expand Down Expand Up @@ -168,10 +174,3 @@ $(GOBIN)/interfacer:
@echo installing: interfacer
@GO111MODULE=on go get -v github.com/rjeczalik/interfaces/cmd/[email protected]
@cd ${cwd}

$(GOBIN)/packr: cwd=$(shell pwd)
$(GOBIN)/packr:
@cd /tmp
@echo installing: packr
@GO111MODULE=on go get -v github.com/gobuffalo/packr/[email protected]
@cd ${cwd}
37 changes: 37 additions & 0 deletions cmd/commands/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package commands

import (
"os"

"github.com/codefresh-io/cli-v2/pkg/config"
"github.com/codefresh-io/cli-v2/pkg/util"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

var (
die = util.Die
exit = os.Exit

cfConfig *config.Config
)

func postInitCommands(commands []*cobra.Command) {
for _, cmd := range commands {
presetRequiredFlags(cmd)
if cmd.HasSubCommands() {
postInitCommands(cmd.Commands())
}
}
}

func presetRequiredFlags(cmd *cobra.Command) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if viper.IsSet(f.Name) && f.Value.String() == "" {
die(cmd.Flags().Set(f.Name, viper.GetString(f.Name)))
}
})
cmd.Flags().SortFlags = false
}
179 changes: 179 additions & 0 deletions cmd/commands/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package commands

import (
"context"
"fmt"
"os"

"github.com/codefresh-io/cli-v2/pkg/log"
"github.com/codefresh-io/cli-v2/pkg/store"
"github.com/codefresh-io/cli-v2/pkg/util"

"github.com/spf13/cobra"
)

func NewConfigCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Manage Codefresh authentication contexts",
PersistentPreRunE: cfConfig.Load,
Long: util.Doc(`By default, Codefresh authentication contexts are persisted at $HOME/.cfconfig.
You can create, delete and list authentication contexts using the following
commands, respectively:

<BIN> config create-context <NAME> --api-key <key>

<BIN> config delete-context <NAME>

<BIN> config get-contexts
`),
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
exit(1)
},
}

cmd.AddCommand(NewConfigGetContextsCommand())
cmd.AddCommand(NewConfigCurrentContextCommand())
cmd.AddCommand(NewConfigUseContextCommand())
cmd.AddCommand(NewConfigCreateContextCommand())
cmd.AddCommand(NewConfigDeleteContextCommand())

return cmd
}

func NewConfigCreateContextCommand() *cobra.Command {
var (
apiKey string
url string
)

cmd := &cobra.Command{
Use: "create-context",
Short: "Create a new Codefresh authentication context",
Example: util.Doc(`
# Create a new context named 'test':

<BIN> config create-context test --api-key TOKEN`),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must provide context name to use")
}
return RunConfigCreateContext(cmd.Context(), args[0], apiKey, url)
},
}

cmd.Flags().StringVar(&apiKey, "api-key", "", "API key")
cmd.Flags().StringVar(&url, "url", store.Get().DefaultAPI, "Codefresh system custom url ")
die(cmd.MarkFlagRequired("api-key"))

return cmd
}

func RunConfigCreateContext(ctx context.Context, context, apiKey, url string) error {
if err := cfConfig.CreateContext(ctx, context, apiKey, url); err != nil {
return err
}
log.G().Infof("create new context: %s", context)
return RunConfigUseContext(ctx, context)
}

func NewConfigGetContextsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "get-contexts",
Aliases: []string{"view"},
Short: "Lists all Codefresh authentication contexts",
Example: util.Doc(`
# List all authentication contexts:

<BIN> config get-contexts`),
RunE: func(cmd *cobra.Command, args []string) error {
return RunConfigGetContexts(cmd.Context())
},
}

return cmd
}

func RunConfigGetContexts(ctx context.Context) error {
return cfConfig.Write(ctx, os.Stdout)
}

func NewConfigCurrentContextCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "current-context",
Short: "Shows the currently selected Codefresh authentication context",
Example: util.Doc(`
# Shows the current context:

<BIN> config current-context`),
RunE: func(cmd *cobra.Command, args []string) error {
return RunConfigCurrentContext(cmd.Context())
},
}

return cmd
}

func RunConfigCurrentContext(ctx context.Context) error {
cur := cfConfig.GetCurrentContext()
if cur.Name == "" {
log.G().Fatal(util.Doc("no currently selected context, use '<BIN> config use-context' to select a context"))
}
log.G().Info(cur.Name)
return nil
}

func NewConfigUseContextCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "use-context CONTEXT",
Short: "Switch the current authentication context",
Example: util.Doc(`
# Switch to another authentication context:

<BIN> config use-context test`),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must provide context name to use")
}
return RunConfigUseContext(cmd.Context(), args[0])
},
}

return cmd
}

func RunConfigUseContext(ctx context.Context, context string) error {
if err := cfConfig.UseContext(ctx, context); err != nil {
return err
}
log.G().Infof("switched to context: %s", context)
return nil
}

func NewConfigDeleteContextCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete-context CONTEXT",
Short: "Delete the specified authentication context",
Example: util.Doc(`
# Deleting an authentication context name 'test':

<BIN> config delete-context test`),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must provide context name to use")
}
return RunConfigDeleteContext(cmd.Context(), args[0])
},
}

return cmd
}

func RunConfigDeleteContext(ctx context.Context, context string) error {
if err := cfConfig.DeleteContext(context); err != nil {
return err
}
log.G().Infof("delete context: %s", context)
return nil
}
27 changes: 5 additions & 22 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package commands

import (
"github.com/codefresh-io/cli-v2/pkg/config"
"github.com/codefresh-io/cli-v2/pkg/store"
"github.com/codefresh-io/cli-v2/pkg/util"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

var die = util.Die

func NewRoot() *cobra.Command {
s := store.Get()

Expand All @@ -35,27 +32,13 @@ variables in advanced to simplify the use of those commands.
DisableAutoGenTag: true, // disable the date in the command docs
}

cfConfig = config.AddFlags(cmd.PersistentFlags())

cmd.AddCommand(NewVersionCommand())
cmd.AddCommand(NewConfigCommand())
cmd.AddCommand(NewRuntimeCommand())

cobra.OnInitialize(func() { postInitCommands(cmd.Commands()) })

return cmd
}

func postInitCommands(commands []*cobra.Command) {
for _, cmd := range commands {
presetRequiredFlags(cmd)
if cmd.HasSubCommands() {
postInitCommands(cmd.Commands())
}
}
}

func presetRequiredFlags(cmd *cobra.Command) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if viper.IsSet(f.Name) && f.Value.String() == "" {
die(cmd.Flags().Set(f.Name, viper.GetString(f.Name)))
}
})
cmd.Flags().SortFlags = false
}
Loading