Skip to content

Commit

Permalink
Merge pull request #35 from CircleCI-Public/source
Browse files Browse the repository at this point in the history
circleci learned `orb source`
  • Loading branch information
eric-hu authored Jul 27, 2018
2 parents 9c741ac + 3418989 commit c46faa4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 19 deletions.
37 changes: 37 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,40 @@ func CreateOrb(ctx context.Context, logger *logger.Logger, name string, namespac
orb, err := createOrbWithNsID(ctx, logger, name, namespaceID)
return orb, err
}

// OrbSource gets the source or an orb
func OrbSource(ctx context.Context, logger *logger.Logger, name string) (string, error) {

var response struct {
Orb struct {
Versions []struct {
Source string
}
}
}

query := `query($name: String!) {
orb(name: $name) {
versions(count: 1) {
source
}
}
}`

request := client.NewAuthorizedRequest(viper.GetString("token"), query)
request.Var("name", name)

graphQLclient := client.NewClient(viper.GetString("endpoint"), logger)

err := graphQLclient.Run(ctx, request, &response)

if err != nil {
return "", err
}

if len(response.Orb.Versions) != 1 {
return "", fmt.Errorf("the %s orb has never published a revision", name)
}

return response.Orb.Versions[0].Source, nil
}
52 changes: 33 additions & 19 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,77 +23,81 @@ var organizationVcs string

func newOrbCommand() *cobra.Command {

orbListCommand := &cobra.Command{
listCommand := &cobra.Command{
Use: "list",
Short: "List orbs",
RunE: listOrbs,
}

orbValidateCommand := &cobra.Command{
validateCommand := &cobra.Command{
Use: "validate [orb.yml]",
Short: "validate an orb.yml",
RunE: validateOrb,
Args: cobra.MaximumNArgs(1),
}

orbExpandCommand := &cobra.Command{
expandCommand := &cobra.Command{
Use: "expand [orb.yml]",
Short: "expand an orb.yml",
RunE: expandOrb,
Args: cobra.MaximumNArgs(1),
}

orbPublishCommand := &cobra.Command{
publishCommand := &cobra.Command{
Use: "publish [orb.yml]",
Short: "publish a version of an orb",
RunE: publishOrb,
Args: cobra.MaximumNArgs(1),
}
orbPublishCommand.Flags().StringVarP(&orbVersion, "orb-version", "o", "", "version of orb to publish")
orbPublishCommand.Flags().StringVarP(&orbID, "orb-id", "i", "", "id of orb to publish")
publishCommand.Flags().StringVarP(&orbVersion, "orb-version", "o", "", "version of orb to publish")
publishCommand.Flags().StringVarP(&orbID, "orb-id", "i", "", "id of orb to publish")

for _, flag := range [2]string{"orb-version", "orb-id"} {
if err := orbPublishCommand.MarkFlagRequired(flag); err != nil {
if err := publishCommand.MarkFlagRequired(flag); err != nil {
panic(err)
}
}

sourceCommand := &cobra.Command{
Use: "source <namespace>/<name>",
Short: "Show the source of an orb",
RunE: showSource,
Args: cobra.ExactArgs(1),
}

orbCreate := &cobra.Command{
Use: "create <namespace>/<name>",
Short: "create an orb",
RunE: createOrb,
Args: cobra.ExactArgs(1),
}

orbCreateNamespace := &cobra.Command{
createNamespace := &cobra.Command{
Use: "create <name>",
Short: "create an orb namespace",
RunE: createOrbNamespace,
Args: cobra.ExactArgs(1),
}
createNamespace.PersistentFlags().StringVar(&organizationName, "org-name", "", "organization name")
createNamespace.PersistentFlags().StringVar(&organizationVcs, "vcs", "github", "organization vcs, e.g. 'github', 'bitbucket'")

namespaceCommand := &cobra.Command{
Use: "ns",
}
namespaceCommand.AddCommand(createNamespace)

orbCommand := &cobra.Command{
Use: "orb",
Short: "Operate on orbs",
}

orbCommand.AddCommand(orbListCommand)
orbCommand.AddCommand(listCommand)
orbCommand.AddCommand(orbCreate)

orbCommand.AddCommand(orbValidateCommand)

orbCommand.AddCommand(orbExpandCommand)

orbCommand.AddCommand(orbPublishCommand)

orbCreateNamespace.PersistentFlags().StringVar(&organizationName, "org-name", "", "organization name")
orbCreateNamespace.PersistentFlags().StringVar(&organizationVcs, "vcs", "github", "organization vcs, e.g. 'github', 'bitbucket'")
namespaceCommand.AddCommand(orbCreateNamespace)
orbCommand.AddCommand(validateCommand)
orbCommand.AddCommand(expandCommand)
orbCommand.AddCommand(publishCommand)
orbCommand.AddCommand(namespaceCommand)
orbCommand.AddCommand(sourceCommand)

return orbCommand
}
Expand Down Expand Up @@ -337,3 +341,13 @@ func createOrbNamespace(cmd *cobra.Command, args []string) error {
Logger.Info("Namespace created")
return nil
}

func showSource(cmd *cobra.Command, args []string) error {
orb := args[0]
source, err := api.OrbSource(context.Background(), Logger, orb)
if err != nil {
return errors.Wrapf(err, "Failed to get source for '%s'", orb)
}
Logger.Info(source)
return nil
}

0 comments on commit c46faa4

Please sign in to comment.