-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rm] Preliminary support to list repositories and components
- Loading branch information
Showing
10 changed files
with
232 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,52 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
nexusrm "github.com/sonatype-nexus-community/gonexus/rm" | ||
) | ||
|
||
var ( | ||
rmClient nexusrm.RM | ||
|
||
// RmCommand is the noun which handles any Nexus Repository actions | ||
RmCommand = &cobra.Command{ | ||
Use: "rm", | ||
Short: "command for managing functionality of Repository Manager.", | ||
Long: `command for managing functionality of Repository Manager.`, | ||
} | ||
RmCommand = func() *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "rm", | ||
Aliases: []string{"r"}, | ||
Short: "Subcommand for managing functionality of Nexus Repository Manager.", | ||
Long: `Subcommand for managing functionality of Nexus Repository Manager.`, | ||
PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
rmClient = newRMClient() | ||
}, | ||
} | ||
|
||
c.PersistentFlags().StringVarP(&rmUser, "user", "u", "", "your Nexus Repository Manager user name.") | ||
c.PersistentFlags().StringVarP(&rmPassword, "password", "p", "", "your Nexus Repository Manager password.") | ||
c.PersistentFlags().StringVarP(&rmServer, "server", "s", "http://localhost", "the address of the Nexus Repository Manager server to use.") | ||
c.PersistentFlags().IntVarP(&rmPort, "port", "", 8081, "the port which the Nexus Repository Manager server is listening on.") | ||
|
||
return c | ||
}() | ||
) | ||
|
||
func init() { | ||
RootCmd.AddCommand(RmCommand) | ||
} | ||
|
||
func newRMClient() nexusrm.RM { | ||
rmServer := viper.GetString("rmServer") | ||
rmPort := viper.GetInt("rmPort") | ||
rmUser := viper.GetString("rmUser") | ||
rmPassword := viper.GetString("rmPassword") | ||
|
||
rmHost := fmt.Sprintf("%s:%d", rmServer, rmPort) | ||
rm, err := nexusrm.New(rmHost, rmUser, rmPassword) | ||
if err != nil { | ||
panic(fmt.Errorf("could not create client to Nexus Repository Manager instance: %v", err)) | ||
} | ||
return rm | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/csv" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
nexusrm "github.com/sonatype-nexus-community/gonexus/rm" | ||
) | ||
|
||
var ( | ||
rmComponentsCmd = &cobra.Command{ | ||
Use: "components", | ||
Aliases: []string{"artifacts", "c"}, | ||
Short: "Manage Nexus Repository Manager components and assets", | ||
Long: `List, create, and remove the components in your Nexus Repository Manager`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
rmListRepoComponents(args) | ||
}, | ||
} | ||
|
||
rmComponentsList = &cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"ls"}, | ||
Short: "List Nexus Repository Manager components", | ||
Long: `List the components in your Nexus Repository Manager in a table`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
rmListRepoComponents(args) | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
RmCommand.AddCommand(rmComponentsCmd) | ||
rmComponentsCmd.AddCommand(rmComponentsList) | ||
} | ||
|
||
func rmListRepoComponents(repos []string) { | ||
w := csv.NewWriter(os.Stdout) | ||
|
||
w.Write([]string{"Repository", "Group", "Name", "Version", "Tags"}) | ||
if len(repos) == 0 { | ||
all, _ := nexusrm.GetRepositories(rmClient) | ||
for _, r := range all { | ||
repos = append(repos, r.Name) | ||
} | ||
} | ||
|
||
for _, repo := range repos { | ||
if components, err := nexusrm.GetComponents(rmClient, repo); err == nil { | ||
for _, c := range components { | ||
w.Write([]string{c.Repository, c.Group, c.Name, c.Version, strings.Join(c.Tags, ";")}) | ||
} | ||
} | ||
} | ||
|
||
w.Flush() | ||
|
||
if err := w.Error(); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
rmUser string | ||
rmPassword string | ||
rmServer string | ||
rmPort int | ||
|
||
rmInitCmd = &cobra.Command{ | ||
Use: "init [-u|--user] username [-p|--password] password ", | ||
Short: "initializes the nexus CLI", | ||
Long: `initializes the nexus CLI and creates a config file (default is $HOME/.nexus.yaml`, | ||
Example: `init --user Dave --password Password123!`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := viper.WriteConfigAs(viper.ConfigFileUsed()) | ||
if err != nil { | ||
fmt.Println(viper.ConfigFileUsed()) | ||
fmt.Println(err) | ||
} else { | ||
fmt.Println() | ||
} | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
rmInitCmd.PersistentFlags().StringVarP(&rmUser, "user", "u", "", "your Nexus Repository Manager user name.") | ||
rmInitCmd.PersistentFlags().StringVarP(&rmPassword, "password", "p", "", "your Nexus Repository Manager password.") | ||
rmInitCmd.PersistentFlags().StringVarP(&rmServer, "server", "s", "http://localhost", "the address of the Nexus Repository Manager server to use.") | ||
rmInitCmd.PersistentFlags().IntVarP(&rmPort, "port", "", 8081, "the port which the Nexus Repository Manager server is listening on.") | ||
|
||
viper.BindPFlag("rmUser", rmInitCmd.PersistentFlags().Lookup("user")) | ||
viper.BindPFlag("rmPassword", rmInitCmd.PersistentFlags().Lookup("password")) | ||
viper.BindPFlag("rmServer", rmInitCmd.PersistentFlags().Lookup("server")) | ||
viper.BindPFlag("rmPort", rmInitCmd.PersistentFlags().Lookup("port")) | ||
|
||
rmInitCmd.MarkFlagRequired("user") | ||
rmInitCmd.MarkFlagRequired("password") | ||
|
||
RmCommand.AddCommand(rmInitCmd) | ||
} |
Oops, something went wrong.