Skip to content

Commit 8af84e5

Browse files
Darkhood148Copilot
andcommitted
Adds harbor context command (goharbor#445)
* adds harbor context command Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * Update cmd/harbor/root/context/list.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Ujjwal Sharma <68021601+Darkhood148@users.noreply.github.com> * renames config to context Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * linting changes Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * documentation changes Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * test changes Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * readd test; handle non-tty in bubble tea Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * rebase and lint Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * rebase and lint Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * rebase and lint Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * highlight active user Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * adds context switch command Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * updates logging Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * updates context switch Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * Update cmd/harbor/root/context/delete.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Ujjwal Sharma <68021601+Darkhood148@users.noreply.github.com> * Update cmd/harbor/root/context/cmd.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Ujjwal Sharma <68021601+Darkhood148@users.noreply.github.com> * minor change Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * rebasing Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * doc changes Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * linting changes Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> * updates docs Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> --------- Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com> Signed-off-by: Ujjwal Sharma <68021601+Darkhood148@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 7f4c33b commit 8af84e5

30 files changed

Lines changed: 475 additions & 164 deletions

cmd/harbor/root/cmd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import (
1818
"io"
1919
"time"
2020

21+
"github.com/goharbor/harbor-cli/cmd/harbor/root/context"
22+
2123
"github.com/goharbor/harbor-cli/cmd/harbor/root/artifact"
22-
"github.com/goharbor/harbor-cli/cmd/harbor/root/config"
2324
"github.com/goharbor/harbor-cli/cmd/harbor/root/cve"
2425
"github.com/goharbor/harbor-cli/cmd/harbor/root/instance"
2526
"github.com/goharbor/harbor-cli/cmd/harbor/root/labels"
@@ -145,7 +146,7 @@ harbor help
145146
root.AddCommand(cmd)
146147

147148
// System
148-
cmd = config.Config()
149+
cmd = context.Context()
149150
cmd.GroupID = "system"
150151
root.AddCommand(cmd)
151152

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
package config
14+
package context
1515

1616
import "github.com/spf13/cobra"
1717

18-
func Config() *cobra.Command {
18+
func Context() *cobra.Command {
1919
cmd := &cobra.Command{
20-
Use: "config",
21-
Short: "Manage the config of the Harbor CLI",
22-
Long: `The config command allows you to manage configurations of the Harbor CLI.
23-
You can add, get, or delete specific config item, as well as list all config items of the Harbor Cli`,
20+
Use: "context",
21+
Short: "Manage locally available contexts",
22+
Example: "harbor context list",
23+
Long: `The context command allows you to manage configuration items of the Harbor CLI.
24+
You can add, get, or delete specific configuration items, as well as list all configuration items of the Harbor CLI.`,
2425
}
26+
2527
cmd.AddCommand(
26-
ListConfigCommand(),
27-
GetConfigItemCommand(),
28-
UpdateConfigItemCommand(),
29-
DeleteConfigItemCommand(),
28+
ListContextCommand(),
29+
GetContextItemCommand(),
30+
UpdateContextItemCommand(),
31+
DeleteContextItemCommand(),
32+
SwitchContextCommand(),
3033
)
3134

3235
return cmd
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
package config
14+
package context
1515

1616
import (
1717
"fmt"
@@ -25,23 +25,23 @@ import (
2525

2626
var deleteCurrent bool
2727

28-
// DeleteConfigItemCommand creates the 'harbor config delete' subcommand,
29-
// allowing you to do: harbor config delete <item>
30-
func DeleteConfigItemCommand() *cobra.Command {
28+
// DeleteContextItemCommand creates the 'harbor context delete' subcommand,
29+
// allowing you to do: harbor context delete <item>
30+
func DeleteContextItemCommand() *cobra.Command {
3131
var credentialName string
3232

3333
cmd := &cobra.Command{
3434
Use: "delete <item>",
3535
Short: "Delete (clear) a specific config item",
3636
Example: `
3737
# Clear the current credential's password
38-
harbor config delete credentials.password
38+
harbor context delete credentials.password
3939
4040
# Clear a specific credential's password using --name
41-
harbor config delete credentials.password --name admin@http://demo.goharbor.io
41+
harbor context delete credentials.password --name admin@http://demo.goharbor.io
4242
4343
# Clear the current credential
44-
harbor config delete --current
44+
harbor context delete --current
4545
`,
4646
Long: `Clear the value of a specific CLI config item by setting it to its zero value.
4747
Case-insensitive field lookup, but uses the canonical (Go) field name internally.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
package config
14+
package context
1515

1616
import (
1717
"encoding/json"
@@ -26,15 +26,15 @@ import (
2626
)
2727

2828
// GetConfigItemCommand creates the 'harbor config get' subcommand.
29-
func GetConfigItemCommand() *cobra.Command {
29+
func GetContextItemCommand() *cobra.Command {
3030
var credentialName string
3131

3232
cmd := &cobra.Command{
3333
Use: "get <item>",
3434
Short: "Get a specific config item",
3535
Example: `
3636
# Get the current credential's username
37-
harbor config get credentials.username
37+
harbor context get credentials.username
3838
3939
# Get a credential's username by specifying the credential name
4040
harbor config get credentials.username --name admin@http://demo.goharbor.io
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
package config
14+
15+
package context
1516

1617
import (
1718
"fmt"
1819

20+
"github.com/goharbor/harbor-cli/pkg/api"
1921
"github.com/goharbor/harbor-cli/pkg/utils"
20-
"github.com/sirupsen/logrus"
22+
"github.com/goharbor/harbor-cli/pkg/views/context/list"
2123
"github.com/spf13/cobra"
2224
"github.com/spf13/viper"
23-
"gopkg.in/yaml.v3"
2425
)
2526

26-
func ListConfigCommand() *cobra.Command {
27+
func ListContextCommand() *cobra.Command {
2728
cmd := &cobra.Command{
2829
Use: "list",
29-
Short: "List config items",
30-
Example: ` harbor config list`,
31-
Long: `Get information of all CLI config items`,
30+
Short: "List contexts",
31+
Example: ` harbor context list`,
3232
Args: cobra.MaximumNArgs(0),
3333
Run: func(cmd *cobra.Command, args []string) {
3434
config, err := utils.GetCurrentHarborConfig()
3535
if err != nil {
36-
logrus.Errorf("Failed to get config: %v", err)
36+
fmt.Println("failed to get config: ", utils.ParseHarborErrorMsg(err))
3737
return
3838
}
3939

@@ -43,19 +43,19 @@ func ListConfigCommand() *cobra.Command {
4343
// Use utils.PrintFormat if available
4444
err = utils.PrintFormat(config, formatFlag)
4545
if err != nil {
46-
logrus.Errorf("Failed to print config: %v", err)
46+
fmt.Println("Failed to print config: ", utils.ParseHarborErrorMsg(err))
47+
return
4748
}
4849
} else {
49-
// Default to YAML format
50-
data, err := yaml.Marshal(config)
51-
if err != nil {
52-
logrus.Errorf("Failed to marshal config to YAML: %v", err)
53-
return
50+
var cxlist []api.ContextListView
51+
for _, cred := range config.Credentials {
52+
cx := api.ContextListView{Name: cred.Name, Username: cred.Username, Server: cred.ServerAddress}
53+
cxlist = append(cxlist, cx)
5454
}
55-
fmt.Println(string(data))
55+
currentCredential := config.CurrentCredentialName
56+
list.ListContexts(cxlist, currentCredential)
5657
}
5758
},
5859
}
59-
6060
return cmd
6161
}

cmd/harbor/root/context/switch.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright Project Harbor Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package context
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/goharbor/harbor-cli/pkg/prompt"
21+
"github.com/goharbor/harbor-cli/pkg/utils"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
func SwitchContextCommand() *cobra.Command {
26+
cmd := &cobra.Command{
27+
Use: "switch <none|context>",
28+
Short: "Switch to a new context",
29+
Example: `harbor context switch harbor-cli@https-demo-goharbor-io`,
30+
Args: cobra.MaximumNArgs(1),
31+
Run: func(cmd *cobra.Command, args []string) {
32+
config, err := utils.GetCurrentHarborConfig()
33+
if err != nil {
34+
fmt.Println("failed to get config: ", utils.ParseHarborErrorMsg(err))
35+
return
36+
}
37+
38+
if len(args) == 1 {
39+
newActiveCredential := args[0]
40+
found := false
41+
42+
for _, cred := range config.Credentials {
43+
if cred.Name == newActiveCredential {
44+
found = true
45+
break
46+
}
47+
}
48+
if found {
49+
config.CurrentCredentialName = newActiveCredential
50+
if err := utils.UpdateConfigFile(config); err != nil {
51+
fmt.Println("failed to update config: ", utils.ParseHarborErrorMsg(err))
52+
}
53+
} else {
54+
fmt.Println("context doesn't exist")
55+
}
56+
} else {
57+
res, err := prompt.GetActiveContextFromUser()
58+
if err != nil {
59+
fmt.Println("failed to get active context: ", utils.ParseHarborErrorMsg(err))
60+
return
61+
}
62+
if res != "" {
63+
config.CurrentCredentialName = res
64+
if err := utils.UpdateConfigFile(config); err != nil {
65+
fmt.Println("failed to update config: ", utils.ParseHarborErrorMsg(err))
66+
}
67+
}
68+
}
69+
},
70+
}
71+
return cmd
72+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
package config
14+
package context
1515

1616
import (
1717
"fmt"
@@ -26,15 +26,15 @@ import (
2626

2727
// UpdateConfigItemCommand creates the 'harbor config update' subcommand,
2828
// allowing you to do: harbor config update <item> <value>.
29-
func UpdateConfigItemCommand() *cobra.Command {
29+
func UpdateContextItemCommand() *cobra.Command {
3030
var credentialName string
3131

3232
cmd := &cobra.Command{
3333
Use: "update <item> <value>",
3434
Short: "Set/update a specific config item",
3535
Example: `
3636
# Set/update the current credential's password
37-
harbor config update credentials.password myNewSecret
37+
harbor context update credentials.password myNewSecret
3838
3939
# Set/update a credential's password by specifying the credential name
4040
harbor config update credentials.password myNewSecret --name admin@http://demo.goharbor.io

doc/cli-docs/harbor-config.md

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: harbor config delete
2+
title: harbor context delete
33
weight: 85
44
---
5-
## harbor config delete
5+
## harbor context delete
66

77
### Description
88

@@ -15,21 +15,21 @@ Case-insensitive field lookup, but uses the canonical (Go) field name internally
1515
If you specify --name, that credential (rather than the "current" one) will be used.
1616

1717
```sh
18-
harbor config delete <item> [flags]
18+
harbor context delete <item> [flags]
1919
```
2020

2121
### Examples
2222

2323
```sh
2424

2525
# Clear the current credential's password
26-
harbor config delete credentials.password
26+
harbor context delete credentials.password
2727

2828
# Clear a specific credential's password using --name
29-
harbor config delete credentials.password --name admin@http://demo.goharbor.io
29+
harbor context delete credentials.password --name admin@http://demo.goharbor.io
3030

3131
# Clear the current credential
32-
harbor config delete --current
32+
harbor context delete --current
3333

3434
```
3535

@@ -51,5 +51,5 @@ harbor config delete <item> [flags]
5151

5252
### SEE ALSO
5353

54-
* [harbor config](harbor-config.md) - Manage the config of the Harbor CLI
54+
* [harbor context](harbor-context.md) - Manage locally available contexts
5555

0 commit comments

Comments
 (0)