diff --git a/cmd/docs.go b/cmd/docs.go index f3d2bbd8..eab184ea 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -11,20 +11,26 @@ var docsCommand = &cobra.Command{ Use: "docs ", Short: "Opens up API documentation for the resource", RunE: func(cmd *cobra.Command, args []string) error { + var err error if len(args) != 0 { resource, ok := resources.GetResourceByName(args[0]) if !ok { return fmt.Errorf("Could not find resource information for resource: %s", args[0]) } - if len(resource.Docs) > 0 { - url := resource.Docs - err := OpenUrl(url) - return err - } else { - return fmt.Errorf("You must supply a valid resource type to the docs command") + switch len(args) { + case 1: + err = openDoc(resource, "") + case 2: + verb := args[1] + err = openDoc(resource, verb) + default: + return doDefault() } } - return fmt.Errorf("You must supply a resource type to the docs command") + if err != nil { + return fmt.Errorf(err.Error()) + } + return nil }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { @@ -37,3 +43,49 @@ var docsCommand = &cobra.Command{ return []string{}, cobra.ShellCompDirectiveNoFileComp }, } + +func openDoc(resourceDoc resources.Resource, verb string) error { + var err error + switch verb { + case "": + if len(resourceDoc.Docs) < 1 { + err = doDefault() + } + err = OpenUrl(resourceDoc.Docs) + case "get-collection": + if resourceDoc.GetCollectionInfo != nil && len(resourceDoc.GetCollectionInfo.Docs) < 1 { + err = doDefault() + } + err = OpenUrl(resourceDoc.GetCollectionInfo.Docs) + case "get": + if resourceDoc.GetEntityInfo != nil && len(resourceDoc.GetEntityInfo.Docs) < 1 { + err = doDefault() + } + err = OpenUrl(resourceDoc.GetEntityInfo.Docs) + case "update": + if resourceDoc.UpdateEntityInfo != nil && len(resourceDoc.UpdateEntityInfo.Docs) < 1 { + err = doDefault() + } + err = OpenUrl(resourceDoc.UpdateEntityInfo.Docs) + case "delete": + if resourceDoc.DeleteEntityInfo != nil && len(resourceDoc.DeleteEntityInfo.Docs) < 1 { + err = doDefault() + } + err = OpenUrl(resourceDoc.DeleteEntityInfo.Docs) + case "create": + if resourceDoc.CreateEntityInfo != nil && len(resourceDoc.CreateEntityInfo.Docs) < 1 { + err = doDefault() + } + err = OpenUrl(resourceDoc.CreateEntityInfo.Docs) + default: + return fmt.Errorf("Could not find verb %s", verb) + + } + if err != nil { + return err + } + return nil +} +func doDefault() error { + return fmt.Errorf(" You must supply a resource type to the docs command") +}