Skip to content

Commit

Permalink
Merge branch 'goharbor:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Roaster05 authored Dec 11, 2024
2 parents 7692212 + 3c16b0c commit 374bd67
Show file tree
Hide file tree
Showing 59 changed files with 2,101 additions and 409 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
args: build-dev --platform linux/amd64 export --path=./harbor-dev

push-latest-images:
if: github.event.pull_request == null && !startsWith(github.ref, 'refs/tags/v')
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/'))
needs:
- lint
- test-code
Expand All @@ -84,7 +84,7 @@ jobs:


publish-release:
if: startsWith(github.ref, 'refs/tags/v')
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/'))
needs:
- lint
- test-code
Expand Down
2 changes: 1 addition & 1 deletion cmd/harbor/root/artifact/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func Artifact() *cobra.Command {

cmd.AddCommand(
ListArtifactCommand(),
InfoArtifactCommmand(),
ViewArtifactCommmand(),
DeleteArtifactCommand(),
ScanArtifactCommand(),
ArtifactTagsCmd(),
Expand Down
39 changes: 0 additions & 39 deletions cmd/harbor/root/artifact/info.go

This file was deleted.

25 changes: 18 additions & 7 deletions cmd/harbor/root/artifact/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
artifactViews "github.com/goharbor/harbor-cli/pkg/views/artifact/list"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ListArtifactCommand() *cobra.Command {
Expand All @@ -19,21 +20,31 @@ func ListArtifactCommand() *cobra.Command {
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var resp artifact.ListArtifactsOK
var artifacts artifact.ListArtifactsOK
var projectName, repoName string

if len(args) > 0 {
projectName, repoName := utils.ParseProjectRepo(args[0])
resp, err = api.ListArtifact(projectName, repoName, opts)
projectName, repoName = utils.ParseProjectRepo(args[0])
} else {
projectName := prompt.GetProjectNameFromUser()
repoName := prompt.GetRepoNameFromUser(projectName)
resp, err = api.ListArtifact(projectName, repoName, opts)
projectName = prompt.GetProjectNameFromUser()
repoName = prompt.GetRepoNameFromUser(projectName)
}

artifacts, err = api.ListArtifact(projectName, repoName, opts)

if err != nil {
log.Errorf("failed to list artifacts: %v", err)
}
artifactViews.ListArtifacts(resp.Payload)

FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
err = utils.PrintFormat(artifacts, FormatFlag)
if err != nil {
log.Error(err)
}
} else {
artifactViews.ListArtifacts(artifacts.Payload)
}
},
}

Expand Down
31 changes: 23 additions & 8 deletions cmd/harbor/root/artifact/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/artifact/tags/create"
"github.com/goharbor/harbor-cli/pkg/views/artifact/tags/list"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ArtifactTagsCmd() *cobra.Command {
Expand Down Expand Up @@ -62,22 +64,35 @@ func ListTagsCmd() *cobra.Command {
Example: `harbor artifact tags list <project>/<repository>/<reference>`,
Run: func(cmd *cobra.Command, args []string) {
var err error
var tags *artifact.ListTagsOK
var projectName, repoName, reference string

var resp artifact.ListTagsOK
if len(args) > 0 {
projectName, repoName, reference := utils.ParseProjectRepoReference(args[0])
resp, err = api.ListTags(projectName, repoName, reference)
projectName, repoName, reference = utils.ParseProjectRepoReference(args[0])
} else {
projectName := prompt.GetProjectNameFromUser()
repoName := prompt.GetRepoNameFromUser(projectName)
reference := prompt.GetReferenceFromUser(repoName, projectName)
resp, err = api.ListTags(projectName, repoName, reference)
projectName = prompt.GetProjectNameFromUser()
repoName = prompt.GetRepoNameFromUser(projectName)
reference = prompt.GetReferenceFromUser(repoName, projectName)
}

tags, err = api.ListTags(projectName, repoName, reference)

if err != nil {
log.Errorf("failed to list tags: %v", err)
return
}

FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
err = utils.PrintFormat(tags, FormatFlag)
if err != nil {
log.Error(err)
return
}
} else {
list.ListTags(tags.Payload)
}

log.Info(resp.Payload)
},
}

Expand Down
55 changes: 55 additions & 0 deletions cmd/harbor/root/artifact/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package artifact

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/artifact/view"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ViewArtifactCommmand() *cobra.Command {

cmd := &cobra.Command{
Use: "view",
Short: "Get information of an artifact",
Long: `Get information of an artifact`,
Example: `harbor artifact view <project>/<repository>/<reference>`,
Run: func(cmd *cobra.Command, args []string) {
var err error
var projectName, repoName, reference string
var artifact *artifact.GetArtifactOK

if len(args) > 0 {
projectName, repoName, reference = utils.ParseProjectRepoReference(args[0])
} else {
projectName = prompt.GetProjectNameFromUser()
repoName = prompt.GetRepoNameFromUser(projectName)
reference = prompt.GetReferenceFromUser(repoName, projectName)
}

artifact, err = api.ViewArtifact(projectName, repoName, reference)

if err != nil {
log.Errorf("failed to get info of an artifact: %v", err)
}

FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
err = utils.PrintFormat(artifact, FormatFlag)
if err != nil {
log.Error(err)
return
}
} else {
view.ViewArtifact(artifact.Payload)
}

},
}

return cmd
}
77 changes: 18 additions & 59 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package root

import (
"fmt"
"log"
"os"

"github.com/goharbor/harbor-cli/cmd/harbor/root/artifact"
"github.com/goharbor/harbor-cli/cmd/harbor/root/labels"
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
repositry "github.com/goharbor/harbor-cli/cmd/harbor/root/repository"
"github.com/goharbor/harbor-cli/cmd/harbor/root/schedule"
"github.com/goharbor/harbor-cli/cmd/harbor/root/user"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/spf13/cobra"
Expand All @@ -21,58 +21,7 @@ var (
verbose bool
)

func InitConfig() {
viper.SetConfigType("yaml")

// cfgFile = viper.GetStering("config")
viper.SetConfigFile(cfgFile)

if cfgFile != utils.DefaultConfigPath {
viper.SetConfigFile(cfgFile)
} else {
stat, err := os.Stat(utils.DefaultConfigPath)
if !os.IsNotExist(err) && stat.Size() == 0 {
log.Println("Config file is empty, creating a new one")
}

if os.IsNotExist(err) {
log.Printf("Config file not found at %s, creating a new one", cfgFile)
}

if os.IsNotExist(err) || (!os.IsNotExist(err) && stat.Size() == 0) {
if _, err := os.Stat(utils.HarborFolder); os.IsNotExist(err) {
// Create the parent directory if it doesn't exist

fmt.Println("Creating config file", utils.HarborFolder)
if err := os.MkdirAll(utils.HarborFolder, os.ModePerm); err != nil {
log.Fatal(err)
}
}
err = utils.CreateConfigFile()

if err != nil {
log.Fatal(err)
}

err = utils.AddCredentialsToConfigFile(utils.Credential{}, cfgFile)

if err != nil {
log.Fatal(err)
}

log.Printf("Config file created at %s", cfgFile)
}
}

if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file: %s", err)
}

}

func RootCmd() *cobra.Command {
utils.SetLocation()

root := &cobra.Command{
Use: "harbor",
Short: "Official Harbor CLI",
Expand All @@ -85,22 +34,30 @@ harbor
// Display help about the command:
harbor help
`,
// RunE: func(cmd *cobra.Command, args []string) error {

// },
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Determine if --config was explicitly set
userSpecifiedConfig := cmd.Flags().Changed("config")
// Initialize configuration
utils.InitConfig(cfgFile, userSpecifiedConfig)

return nil
},
}

cobra.OnInitialize(InitConfig)

root.PersistentFlags().StringVarP(&output, "output-format", "o", "", "Output format. One of: json|yaml")
root.PersistentFlags().StringVar(&cfgFile, "config", utils.DefaultConfigPath, "config file (default is $HOME/.harbor/config.yaml)")
root.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/harbor-cli/config.yaml)")
root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")

err := viper.BindPFlag("output-format", root.PersistentFlags().Lookup("output-format"))
if err != nil {
fmt.Println(err.Error())
}

err = viper.BindPFlag("config", root.PersistentFlags().Lookup("config"))
if err != nil {
fmt.Println(err.Error())
}

root.AddCommand(
versionCommand(),
LoginCommand(),
Expand All @@ -110,6 +67,8 @@ harbor help
user.User(),
artifact.Artifact(),
HealthCommand(),
schedule.Schedule(),
labels.Labels(),
)

return root
Expand Down
1 change: 0 additions & 1 deletion cmd/harbor/root/labels/add.go

This file was deleted.

6 changes: 6 additions & 0 deletions cmd/harbor/root/labels/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ func Labels() *cobra.Command {
Use: "label",
Short: "Manage labels in Harbor",
}
cmd.AddCommand(
CreateLabelCommand(),
DeleteLabelCommand(),
ListLabelCommand(),
UpdateLableCommand(),
)

return cmd
}
Loading

0 comments on commit 374bd67

Please sign in to comment.