Skip to content

Commit

Permalink
Orbs command now provides latest version if needed (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
felicianotech committed Feb 22, 2023
1 parent 0268153 commit 4021ec8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
68 changes: 62 additions & 6 deletions arc/cmd/orbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,87 @@ package cmd
import (
"fmt"
"io/ioutil"
"strings"

"golang.org/x/mod/semver"

"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
)

// a partial example of a CircleCI configuration file
type cciConfig struct {
Orbs map[string]string `yaml:"orbs"`
}

// a CircleCI orb
type orb struct {
name string
namespace string
version string
LatestVersion string `json:"version,omitempty"`
ObjectID string `json:"objectID,omitempty"`
}

func orbParse(definition string) orb {

slash := strings.Index(definition, "/")
at := strings.Index(definition, "@")

return orb{
namespace: definition[0:slash],
name: definition[slash+1 : at],
version: definition[at+1:],
}
}

var orbsCmd = &cobra.Command{
Use: "orbs",
Short: "Display what orbs your config is using.",
Run: func(cmd *cobra.Command, args []string) {

cciConfig, err := ioutil.ReadFile(".circleci/config.yml")
configFile, err := ioutil.ReadFile(".circleci/config.yml")
if err != nil {
fmt.Printf("yamlFile.Get err #%v ", err)
}

yMap := make(map[interface{}]interface{})
var config cciConfig

err = yaml.Unmarshal(cciConfig, &yMap)
err = yaml.Unmarshal(configFile, &config)
if err != nil {
fmt.Printf(err.Error())
}

count := len(yMap["orbs"].(map[string]interface{}))
fmt.Printf("This config is using %d orbs.", count)
fmt.Printf(" Orbs is %s", yMap["orbs"])
count := len(config.Orbs)

if count == 0 {
fmt.Println("This config doesn't use any orbs.")
return
}

fmt.Printf("This config is using %d orbs.\n\n", count)

client := search.NewClient("U0RXNGRK45", "0efdf785bc8c215d9f5fd7fdece50795")
index := client.InitIndex("orbs-prod")

for _, orbStr := range config.Orbs {

anOrb := orbParse(orbStr)
err := index.GetObject(anOrb.namespace+"/"+anOrb.name, &anOrb)
if err != nil {
fmt.Printf(err.Error())
}

fmt.Printf("%s/%s: v%s", anOrb.namespace, anOrb.name, anOrb.version)

if semver.Compare("v"+anOrb.version, "v"+anOrb.LatestVersion) == -1 {
fmt.Printf(" (latest v%s)", anOrb.LatestVersion)
}

//intentionally empty
fmt.Println()
}
},
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ module github.com/hubci/arc
go 1.20

require (
github.com/algolia/algoliasearch-client-go/v3 v3.26.3
github.com/goccy/go-yaml v1.9.8
github.com/magefile/mage v1.14.0
github.com/repowarden/cli v0.11.1
github.com/spf13/cobra v1.6.1
golang.org/x/mod v0.8.0
)

require (
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/algolia/algoliasearch-client-go/v3 v3.26.3 h1:H8ybfEGnv0/boLlTROA+L3lDqjQ5XvzytC9Vbi//VUo=
github.com/algolia/algoliasearch-client-go/v3 v3.26.3/go.mod h1:i7tLoP7TYDmHX3Q7vkIOL4syVse/k5VJ+k0i8WqFiJk=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
Expand All @@ -26,6 +30,7 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/repowarden/cli v0.11.1 h1:JYEpfPuf3pI/4a9Wg3Yn9tuGBra7MiA/5/aMErWd9j8=
github.com/repowarden/cli v0.11.1/go.mod h1:nxYH7mdrEje/ZC9QsDVuw40kjZEMmFBnOH+eeXv+Fvw=
Expand All @@ -36,11 +41,15 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
golang.org/x/exp v0.0.0-20230111222715-75897c7a292a h1:/YWeLOBWYV5WAQORVPkZF3Pq9IppkcT72GKnWjNf5W8=
golang.org/x/exp v0.0.0-20230111222715-75897c7a292a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -58,4 +67,6 @@ golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3j
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 4021ec8

Please sign in to comment.