Skip to content

Commit

Permalink
[rm] Preliminary support to list repositories and components
Browse files Browse the repository at this point in the history
  • Loading branch information
HokieGeek committed Nov 27, 2019
1 parent 68258a1 commit 8bfbf9d
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 46 deletions.
12 changes: 9 additions & 3 deletions cmd/iq.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ import (
)

var (
iqClient nexusiq.IQ

// IqCommand is the noun which handles any Nexus IQ actions
IqCommand = &cobra.Command{
Use: "iq",
Short: "command for managing functionality of Nexus IQ",
Long: `command for managing functionality of Nexus IQ`,
Use: "iq",
Aliases: []string{"q"},
Short: "Subcommand for managing functionality of Nexus IQ",
Long: `Subcommand for managing functionality of Nexus IQ`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
iqClient = newIQClient()
},
}
)

Expand Down
16 changes: 5 additions & 11 deletions cmd/iq_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,44 +73,38 @@ func init() {
}

func iqAppsList() {
iq := newIQClient()

fmt.Printf("%s, %s, %s, %s\n", "Name", "Public ID", "ID", "Organization ID")
orgsID2Name, _, _ := iqOrgsIDMap(iq)
if apps, err := nexusiq.GetAllApplications(iq); err == nil {
orgsID2Name, _, _ := iqOrgsIDMap(iqClient)
if apps, err := nexusiq.GetAllApplications(iqClient); err == nil {
for _, a := range apps {
fmt.Printf("%s, %s, %s, %s\n", a.Name, a.PublicID, a.ID, orgsID2Name[a.OrganizationID])
}
}
}

func iqAppsCreate(name, id, organization string) {
iq := newIQClient()

org, err := nexusiq.GetOrganizationByName(iq, organization)
org, err := nexusiq.GetOrganizationByName(iqClient, organization)
if err != nil {
fmt.Fprintf(os.Stderr, "error finding organization '%s': %v\n", organization, err)
return
}

if _, err = nexusiq.CreateApplication(iq, name, id, org.ID); err != nil {
if _, err = nexusiq.CreateApplication(iqClient, name, id, org.ID); err != nil {
fmt.Fprintf(os.Stderr, "error creating application '%s': %v\n", name, err)
return
}
fmt.Printf("Created application %s (%s) in %s\n", name, id, organization)
}

func iqAppsDelete(ids []string) {
iq := newIQClient()

type catcher struct {
id string
err error
}

errs := make([]catcher, 0)
for _, id := range ids {
if err := nexusiq.DeleteApplication(iq, id); err != nil {
if err := nexusiq.DeleteApplication(iqClient, id); err != nil {
errs = append(errs, catcher{id, err})
continue
}
Expand Down
18 changes: 5 additions & 13 deletions cmd/iq_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ func init() {
}

func iqComponentDeets(format string, ids ...string) {
iq := newIQClient()

type catcher struct {
id string
err error
Expand All @@ -133,7 +131,7 @@ func iqComponentDeets(format string, ids ...string) {
c, err := nexusiq.NewComponentFromString(id)
var components []nexusiq.ComponentDetail
if err == nil {
components, err = nexusiq.GetComponents(iq, []nexusiq.Component{*c})
components, err = nexusiq.GetComponents(iqClient, []nexusiq.Component{*c})
}
if err != nil {
errs = append(errs, catcher{id, err})
Expand All @@ -159,9 +157,7 @@ func iqComponentDeets(format string, ids ...string) {
}

func iqComponentsAll(format string) {
iq := newIQClient()

components, err := nexusiq.GetAllComponents(iq)
components, err := nexusiq.GetAllComponents(iqClient)
if err != nil {
log.Printf("error listing components: %v\n", err)
return
Expand All @@ -181,8 +177,6 @@ func iqComponentsAll(format string) {
}

func iqComponentEval(format, app string, components []string) {
iq := newIQClient()

comps := make([]nexusiq.Component, len(components))
for i, c := range components {
comps[i] = nexusiq.Component{PackageURL: c}
Expand All @@ -191,9 +185,9 @@ func iqComponentEval(format, app string, components []string) {
var report *nexusiq.Evaluation
var err error
if app != "" {
report, err = nexusiq.EvaluateComponents(iq, comps, app)
report, err = nexusiq.EvaluateComponents(iqClient, comps, app)
} else {
report, err = privateiq.EvaluateComponentsWithRootOrg(iq, comps)
report, err = privateiq.EvaluateComponentsWithRootOrg(iqClient, comps)
}
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -226,8 +220,6 @@ func getRemediationByPURL(iq nexusiq.IQ, application, organization, stage, compo
}

func iqComponentRemediation(format, application, organization, stage string, components []string) {
iq := newIQClient()

type catcher struct {
component string
err error
Expand All @@ -238,7 +230,7 @@ func iqComponentRemediation(format, application, organization, stage string, com
errs := make([]catcher, 0)
for _, c := range components {

remediation, err := getRemediationByPURL(iq, application, organization, stage, c)
remediation, err := getRemediationByPURL(iqClient, application, organization, stage, c)
if err != nil {
errs = append(errs, catcher{c, err})
continue
Expand Down
3 changes: 1 addition & 2 deletions cmd/iq_license.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func iqInstallLicense(licensePath string) {
panic(err)
}

iq := newIQClient()
if err = privateiq.InstallLicense(iq, license); err != nil {
if err = privateiq.InstallLicense(iqClient, license); err != nil {
panic(err)
}

Expand Down
10 changes: 3 additions & 7 deletions cmd/iq_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ func iqOrgsList() {
}

func iqOrgsCreate(names []string) {
iq := newIQClient()

type catcher struct {
name string
err error
}

errs := make([]catcher, 0)
for _, name := range names {
orgID, err := nexusiq.CreateOrganization(iq, name)
orgID, err := nexusiq.CreateOrganization(iqClient, name)
if err != nil {
errs = append(errs, catcher{name, err})
continue
Expand All @@ -90,18 +88,16 @@ func iqOrgsCreate(names []string) {
}

func iqOrgsDelete(names []string) {
iq := newIQClient()

type catcher struct {
name string
err error
}

errs := make([]catcher, 0)
for _, name := range names {
org, err := nexusiq.GetOrganizationByName(iq, name)
org, err := nexusiq.GetOrganizationByName(iqClient, name)
if err == nil {
err = privateiq.DeleteOrganization(iq, org.ID)
err = privateiq.DeleteOrganization(iqClient, org.ID)
}
if err != nil {
errs = append(errs, catcher{name, err})
Expand Down
44 changes: 39 additions & 5 deletions cmd/rm.go
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
}
64 changes: 64 additions & 0 deletions cmd/rm_components.go
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)
}
}
48 changes: 48 additions & 0 deletions cmd/rm_init.go
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)
}
Loading

0 comments on commit 8bfbf9d

Please sign in to comment.