Skip to content

Commit 0720533

Browse files
authored
Install runtime (#3)
1 parent 303c840 commit 0720533

30 files changed

+3101
-92
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dist/
2727

2828
# Icon must end with two \r
2929
Icon
30+
vendor
3031

3132

3233
# Thumbnails

Makefile

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1-
VERSION=v0.0.7
1+
VERSION=v0.0.8
22
OUT_DIR=dist
33

44
CLI_NAME?=cf
55
IMAGE_REPOSITORY?=quay.io
66
IMAGE_NAMESPACE?=codefresh
77

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

11-
DEV_INSTALLATION_MANIFESTS_URL="manifests/"
12-
DEV_INSTALLATION_MANIFESTS_NAMESPACED_URL="manifests/namespace-install"
13+
DEV_ARGOCD_INSTALLATION_MANIFESTS_URL="manifests/argo-cd"
14+
DEV_EVENTS_INSTALLATION_MANIFESTS_URL="manifests/argo-events"
15+
DEV_ROLLOUTS_INSTALLATION_MANIFESTS_URL="manifests/argo-rollouts"
16+
DEV_WORKFLOWS_INSTALLATION_MANIFESTS_URL="manifests/argo-workflows"
1317

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

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

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

2225
DEV_MODE?=true
2326

2427
ifeq (${DEV_MODE},true)
25-
INSTALLATION_MANIFESTS_URL=${DEV_INSTALLATION_MANIFESTS_URL}
26-
INSTALLATION_MANIFESTS_NAMESPACED_URL=${DEV_INSTALLATION_MANIFESTS_NAMESPACED_URL}
28+
ARGOCD_INSTALLATION_MANIFESTS_URL=${ARGOCD_DEV_INSTALLATION_MANIFESTS_URL}
29+
EVENTS_INSTALLATION_MANIFESTS_URL=${EVENTS_DEV_INSTALLATION_MANIFESTS_URL}
30+
ROLLOUTS_INSTALLATION_MANIFESTS_URL=${ROLLOUTS_DEV_INSTALLATION_MANIFESTS_URL}
31+
WORKFLOWS_INSTALLATION_MANIFESTS_URL=${WORKFLOWS_DEV_INSTALLATION_MANIFESTS_URL}
2732
endif
2833

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

84-
$(OUT_DIR)/$(CLI_NAME)-%: $(CLI_SRCS) $(GOBIN)/packr
89+
$(OUT_DIR)/$(CLI_NAME)-%: $(CLI_SRCS)
8590
@GO_FLAGS=$(GO_FLAGS) \
8691
BUILD_DATE=$(BUILD_DATE) \
8792
BINARY_NAME=$(CLI_NAME) \
8893
VERSION=$(VERSION) \
8994
GIT_COMMIT=$(GIT_COMMIT) \
90-
PACKR_CMD=$(PACKR_CMD) \
9195
OUT_FILE=$(OUT_DIR)/$(CLI_NAME)-$* \
92-
INSTALLATION_MANIFESTS_URL=$(INSTALLATION_MANIFESTS_URL) \
93-
INSTALLATION_MANIFESTS_NAMESPACED_URL=$(INSTALLATION_MANIFESTS_NAMESPACED_URL) \
96+
ARGOCD_INSTALLATION_MANIFESTS_URL=$(ARGOCD_INSTALLATION_MANIFESTS_URL) \
97+
EVENTS_INSTALLATION_MANIFESTS_URL=$(EVENTS_INSTALLATION_MANIFESTS_URL) \
98+
ROLLOUTS_INSTALLATION_MANIFESTS_URL=$(ROLLOUTS_INSTALLATION_MANIFESTS_URL) \
99+
WORKFLOWS_INSTALLATION_MANIFESTS_URL=$(WORKFLOWS_INSTALLATION_MANIFESTS_URL) \
94100
MAIN=./cmd \
95101
./hack/build.sh
96102

@@ -168,10 +174,3 @@ $(GOBIN)/interfacer:
168174
@echo installing: interfacer
169175
@GO111MODULE=on go get -v github.com/rjeczalik/interfaces/cmd/interfacer@v0.1.1
170176
@cd ${cwd}
171-
172-
$(GOBIN)/packr: cwd=$(shell pwd)
173-
$(GOBIN)/packr:
174-
@cd /tmp
175-
@echo installing: packr
176-
@GO111MODULE=on go get -v github.com/gobuffalo/packr/packr@v1.30.1
177-
@cd ${cwd}

cmd/commands/common.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package commands
2+
3+
import (
4+
"os"
5+
6+
"github.com/codefresh-io/cli-v2/pkg/config"
7+
"github.com/codefresh-io/cli-v2/pkg/util"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/pflag"
11+
"github.com/spf13/viper"
12+
)
13+
14+
var (
15+
die = util.Die
16+
exit = os.Exit
17+
18+
cfConfig *config.Config
19+
)
20+
21+
func postInitCommands(commands []*cobra.Command) {
22+
for _, cmd := range commands {
23+
presetRequiredFlags(cmd)
24+
if cmd.HasSubCommands() {
25+
postInitCommands(cmd.Commands())
26+
}
27+
}
28+
}
29+
30+
func presetRequiredFlags(cmd *cobra.Command) {
31+
cmd.Flags().VisitAll(func(f *pflag.Flag) {
32+
if viper.IsSet(f.Name) && f.Value.String() == "" {
33+
die(cmd.Flags().Set(f.Name, viper.GetString(f.Name)))
34+
}
35+
})
36+
cmd.Flags().SortFlags = false
37+
}

cmd/commands/config.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package commands
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/codefresh-io/cli-v2/pkg/log"
9+
"github.com/codefresh-io/cli-v2/pkg/store"
10+
"github.com/codefresh-io/cli-v2/pkg/util"
11+
12+
"github.com/spf13/cobra"
13+
)
14+
15+
func NewConfigCommand() *cobra.Command {
16+
cmd := &cobra.Command{
17+
Use: "config",
18+
Short: "Manage Codefresh authentication contexts",
19+
PersistentPreRunE: cfConfig.Load,
20+
Long: util.Doc(`By default, Codefresh authentication contexts are persisted at $HOME/.cfconfig.
21+
You can create, delete and list authentication contexts using the following
22+
commands, respectively:
23+
24+
<BIN> config create-context <NAME> --api-key <key>
25+
26+
<BIN> config delete-context <NAME>
27+
28+
<BIN> config get-contexts
29+
`),
30+
Run: func(cmd *cobra.Command, args []string) {
31+
cmd.HelpFunc()(cmd, args)
32+
exit(1)
33+
},
34+
}
35+
36+
cmd.AddCommand(NewConfigGetContextsCommand())
37+
cmd.AddCommand(NewConfigCurrentContextCommand())
38+
cmd.AddCommand(NewConfigUseContextCommand())
39+
cmd.AddCommand(NewConfigCreateContextCommand())
40+
cmd.AddCommand(NewConfigDeleteContextCommand())
41+
42+
return cmd
43+
}
44+
45+
func NewConfigCreateContextCommand() *cobra.Command {
46+
var (
47+
apiKey string
48+
url string
49+
)
50+
51+
cmd := &cobra.Command{
52+
Use: "create-context",
53+
Short: "Create a new Codefresh authentication context",
54+
Example: util.Doc(`
55+
# Create a new context named 'test':
56+
57+
<BIN> config create-context test --api-key TOKEN`),
58+
RunE: func(cmd *cobra.Command, args []string) error {
59+
if len(args) < 1 {
60+
return fmt.Errorf("must provide context name to use")
61+
}
62+
return RunConfigCreateContext(cmd.Context(), args[0], apiKey, url)
63+
},
64+
}
65+
66+
cmd.Flags().StringVar(&apiKey, "api-key", "", "API key")
67+
cmd.Flags().StringVar(&url, "url", store.Get().DefaultAPI, "Codefresh system custom url ")
68+
die(cmd.MarkFlagRequired("api-key"))
69+
70+
return cmd
71+
}
72+
73+
func RunConfigCreateContext(ctx context.Context, context, apiKey, url string) error {
74+
if err := cfConfig.CreateContext(ctx, context, apiKey, url); err != nil {
75+
return err
76+
}
77+
log.G().Infof("create new context: %s", context)
78+
return RunConfigUseContext(ctx, context)
79+
}
80+
81+
func NewConfigGetContextsCommand() *cobra.Command {
82+
cmd := &cobra.Command{
83+
Use: "get-contexts",
84+
Aliases: []string{"view"},
85+
Short: "Lists all Codefresh authentication contexts",
86+
Example: util.Doc(`
87+
# List all authentication contexts:
88+
89+
<BIN> config get-contexts`),
90+
RunE: func(cmd *cobra.Command, args []string) error {
91+
return RunConfigGetContexts(cmd.Context())
92+
},
93+
}
94+
95+
return cmd
96+
}
97+
98+
func RunConfigGetContexts(ctx context.Context) error {
99+
return cfConfig.Write(ctx, os.Stdout)
100+
}
101+
102+
func NewConfigCurrentContextCommand() *cobra.Command {
103+
cmd := &cobra.Command{
104+
Use: "current-context",
105+
Short: "Shows the currently selected Codefresh authentication context",
106+
Example: util.Doc(`
107+
# Shows the current context:
108+
109+
<BIN> config current-context`),
110+
RunE: func(cmd *cobra.Command, args []string) error {
111+
return RunConfigCurrentContext(cmd.Context())
112+
},
113+
}
114+
115+
return cmd
116+
}
117+
118+
func RunConfigCurrentContext(ctx context.Context) error {
119+
cur := cfConfig.GetCurrentContext()
120+
if cur.Name == "" {
121+
log.G().Fatal(util.Doc("no currently selected context, use '<BIN> config use-context' to select a context"))
122+
}
123+
log.G().Info(cur.Name)
124+
return nil
125+
}
126+
127+
func NewConfigUseContextCommand() *cobra.Command {
128+
cmd := &cobra.Command{
129+
Use: "use-context CONTEXT",
130+
Short: "Switch the current authentication context",
131+
Example: util.Doc(`
132+
# Switch to another authentication context:
133+
134+
<BIN> config use-context test`),
135+
RunE: func(cmd *cobra.Command, args []string) error {
136+
if len(args) < 1 {
137+
return fmt.Errorf("must provide context name to use")
138+
}
139+
return RunConfigUseContext(cmd.Context(), args[0])
140+
},
141+
}
142+
143+
return cmd
144+
}
145+
146+
func RunConfigUseContext(ctx context.Context, context string) error {
147+
if err := cfConfig.UseContext(ctx, context); err != nil {
148+
return err
149+
}
150+
log.G().Infof("switched to context: %s", context)
151+
return nil
152+
}
153+
154+
func NewConfigDeleteContextCommand() *cobra.Command {
155+
cmd := &cobra.Command{
156+
Use: "delete-context CONTEXT",
157+
Short: "Delete the specified authentication context",
158+
Example: util.Doc(`
159+
# Deleting an authentication context name 'test':
160+
161+
<BIN> config delete-context test`),
162+
RunE: func(cmd *cobra.Command, args []string) error {
163+
if len(args) < 1 {
164+
return fmt.Errorf("must provide context name to use")
165+
}
166+
return RunConfigDeleteContext(cmd.Context(), args[0])
167+
},
168+
}
169+
170+
return cmd
171+
}
172+
173+
func RunConfigDeleteContext(ctx context.Context, context string) error {
174+
if err := cfConfig.DeleteContext(context); err != nil {
175+
return err
176+
}
177+
log.G().Infof("delete context: %s", context)
178+
return nil
179+
}

cmd/commands/root.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package commands
22

33
import (
4+
"github.com/codefresh-io/cli-v2/pkg/config"
45
"github.com/codefresh-io/cli-v2/pkg/store"
56
"github.com/codefresh-io/cli-v2/pkg/util"
67

78
"github.com/spf13/cobra"
8-
"github.com/spf13/pflag"
9-
"github.com/spf13/viper"
109
)
1110

12-
var die = util.Die
13-
1411
func NewRoot() *cobra.Command {
1512
s := store.Get()
1613

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

35+
cfConfig = config.AddFlags(cmd.PersistentFlags())
36+
3837
cmd.AddCommand(NewVersionCommand())
38+
cmd.AddCommand(NewConfigCommand())
39+
cmd.AddCommand(NewRuntimeCommand())
3940

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

4243
return cmd
4344
}
44-
45-
func postInitCommands(commands []*cobra.Command) {
46-
for _, cmd := range commands {
47-
presetRequiredFlags(cmd)
48-
if cmd.HasSubCommands() {
49-
postInitCommands(cmd.Commands())
50-
}
51-
}
52-
}
53-
54-
func presetRequiredFlags(cmd *cobra.Command) {
55-
cmd.Flags().VisitAll(func(f *pflag.Flag) {
56-
if viper.IsSet(f.Name) && f.Value.String() == "" {
57-
die(cmd.Flags().Set(f.Name, viper.GetString(f.Name)))
58-
}
59-
})
60-
cmd.Flags().SortFlags = false
61-
}

0 commit comments

Comments
 (0)