本文整理匯總了Golang中github.com/coreos/rkt/Godeps/_workspace/src/github.com/spf13/cobra.Command類的典型用法代碼示例。如果您正苦於以下問題:Golang Command類的具體用法?Golang Command怎麽用?Golang Command使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Command類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: runCatManifest
func runCatManifest(cmd *cobra.Command, args []string) (exit int) {
if len(args) != 1 {
cmd.Usage()
return 1
}
pod, err := getPodFromUUIDString(args[0])
if err != nil {
stderr("cat-manifest: problem retrieving pod: %v", err)
return 1
}
defer pod.Close()
manifest, err := pod.getManifest()
if err != nil {
return 1
}
var b []byte
if flagPMPrettyPrint {
b, err = json.MarshalIndent(manifest, "", "\t")
} else {
b, err = json.Marshal(manifest)
}
if err != nil {
stderr("cat-manifest: cannot read the pod manifest: %v", err)
return 1
}
stdout(string(b))
return 0
}
示例2: runStatus
func runStatus(cmd *cobra.Command, args []string) (exit int) {
if len(args) != 1 {
cmd.Usage()
return 1
}
p, err := getPodFromUUIDString(args[0])
if err != nil {
stderr("Problem retrieving pod: %v", err)
return 1
}
defer p.Close()
if flagWait {
if err := p.waitExited(); err != nil {
stderr("Unable to wait for pod: %v", err)
return 1
}
}
if err = printStatus(p); err != nil {
stderr("Unable to print status: %v", err)
return 1
}
return 0
}
示例3: runTrust
func runTrust(cmd *cobra.Command, args []string) (exit int) {
if flagPrefix == "" && !flagRoot {
if len(args) != 0 {
stderr("--root required for non-prefixed (root) keys")
} else {
cmd.Usage()
}
return 1
}
if flagPrefix != "" && flagRoot {
stderr("--root and --prefix usage mutually exclusive")
return 1
}
// if the user included a scheme with the prefix, error on it
u, err := url.Parse(flagPrefix)
if err == nil && u.Scheme != "" {
stderr("--prefix must not contain a URL scheme, omit %s://", u.Scheme)
return 1
}
pkls, err := getPubKeyLocations(flagPrefix, args)
if err != nil {
stderr("Error determining key location: %v", err)
return 1
}
if err := addKeys(pkls, flagPrefix); err != nil {
stderr("Error adding keys: %v", err)
return 1
}
return 0
}
示例4: runRm
func runRm(cmd *cobra.Command, args []string) (exit int) {
var podUUID *types.UUID
var err error
switch {
case len(args) == 0 && flagUUIDFile != "":
podUUID, err = readUUIDFromFile(flagUUIDFile)
if err != nil {
stderr("Unable to read UUID from file: %v", err)
return 1
}
case len(args) == 1 && flagUUIDFile == "":
podUUID, err = resolveUUID(args[0])
if err != nil {
stderr("Unable to resolve UUID: %v", err)
return 1
}
default:
cmd.Usage()
return 1
}
p, err := getPod(podUUID)
if err != nil {
stderr("Cannot get pod: %v", err)
return 1
}
return removePod(p)
}
示例5: runStatus
func runStatus(cmd *cobra.Command, args []string) (exit int) {
if len(args) != 1 {
cmd.Usage()
return 1
}
podUUID, err := resolveUUID(args[0])
if err != nil {
stderr("Unable to resolve UUID: %v", err)
return 1
}
p, err := getPod(podUUID)
if err != nil {
stderr("Unable to get pod: %v", err)
return 1
}
defer p.Close()
if flagWait {
if err := p.waitExited(); err != nil {
stderr("Unable to wait for pod: %v", err)
return 1
}
}
if err = printStatus(p); err != nil {
stderr("Unable to print status: %v", err)
return 1
}
return 0
}
示例6: runTrust
func runTrust(cmd *cobra.Command, args []string) (exit int) {
if globalFlags.InsecureSkipVerify {
// --insecure-skip-verify disable the keystore but we need it for rkt trust
stderr("--insecure-skip-verify cannot be used with rkt trust")
return 1
}
if flagPrefix == "" && !flagRoot {
if len(args) != 0 {
stderr("--root required for non-prefixed (root) keys")
} else {
cmd.Usage()
}
return 1
}
if flagPrefix != "" && flagRoot {
stderr("--root and --prefix usage mutually exclusive")
return 1
}
ks := getKeystore()
if ks == nil {
stderr("could not get the keystore")
return 1
}
// if the user included a scheme with the prefix, error on it
u, err := url.Parse(flagPrefix)
if err == nil && u.Scheme != "" {
stderr("--prefix must not contain a URL scheme, omit %s://", u.Scheme)
return 1
}
pkls := args
m := &pubkey.Manager{
InsecureAllowHttp: flagAllowHTTP,
TrustKeysFromHttps: globalFlags.TrustKeysFromHttps,
Ks: ks,
Debug: globalFlags.Debug,
}
if len(pkls) == 0 {
pkls, err = m.GetPubKeyLocations(flagPrefix)
if err != nil {
stderr("Error determining key location: %v", err)
return 1
}
}
acceptOpt := pubkey.AcceptAsk
if globalFlags.InsecureSkipVerify {
acceptOpt = pubkey.AcceptForce
}
if err := m.AddKeys(pkls, flagPrefix, acceptOpt, pubkey.OverrideDeny); err != nil {
stderr("Error adding keys: %v", err)
return 1
}
return 0
}
示例7: runImageImport
func runImageImport(cmd *cobra.Command, args []string) (exit int) {
if len(args) != 2 {
cmd.Usage()
return 1
}
outFile := args[1]
ext := filepath.Ext(outFile)
if ext != schema.ACIExtension {
stderr("rkt: Extension must be %s (given %s)", schema.ACIExtension, ext)
return 1
}
aciImgPath, err := oci2aciImage(args[0])
if err != nil {
stderr("rkt: oci2aci failed: %s", err)
return 1
}
if err = run(exec.Command("mv", aciImgPath, args[1])); err != nil {
return 1
}
return 0
}
示例8: runImageImport
func runImageImport(cmd *cobra.Command, args []string) (exit int) {
if len(args) != 1 {
cmd.Usage()
return 1
}
//convert oci bundle to aci image
aciImage, err := oci2aci.Oci2aciImage(args[0])
if err != nil {
fmt.Printf("oci2aci failed: %v", err)
return 1
}
//save aci to rkt store
s, err := store.NewStore(globalFlags.Dir)
if err != nil {
fmt.Printf("cannot open store: %v", err)
return 1
}
aciFile, err := os.Open(aciImage)
if err != nil {
fmt.Printf("opening ACI file %s failed: %v", aciImage, err)
return 1
}
key, err := s.WriteACI(aciFile, true)
if err != nil {
fmt.Printf("write ACI file failed: %v", err)
return 1
}
fmt.Println(key)
return 0
}
示例9: runFetch
func runFetch(cmd *cobra.Command, args []string) (exit int) {
if err := parseApps(&rktApps, args, cmd.Flags(), false); err != nil {
stderr("fetch: unable to parse arguments: %v", err)
return 1
}
if rktApps.Count() < 1 {
stderr("fetch: must provide at least one image")
return 1
}
if flagStoreOnly && flagNoStore {
stderr("both --store-only and --no-store specified")
return 1
}
s, err := store.NewStore(getDataDir())
if err != nil {
stderr("fetch: cannot open store: %v", err)
return 1
}
ks := getKeystore()
config, err := getConfig()
if err != nil {
stderr("fetch: cannot get configuration: %v", err)
return 1
}
ft := &image.Fetcher{
S: s,
Ks: ks,
Headers: config.AuthPerHost,
DockerAuth: config.DockerCredentialsPerRegistry,
InsecureFlags: globalFlags.InsecureFlags,
Debug: globalFlags.Debug,
TrustKeysFromHTTPS: globalFlags.TrustKeysFromHTTPS,
StoreOnly: flagStoreOnly,
NoStore: flagNoStore,
WithDeps: true,
}
err = rktApps.Walk(func(app *apps.App) error {
hash, err := ft.FetchImage(app.Image, app.Asc, app.ImType)
if err != nil {
return err
}
if !flagFullHash {
hash = types.ShortHash(hash)
}
stdout(hash)
return nil
})
if err != nil {
stderr("%v", err)
return 1
}
return
}
示例10: getSubCommands
func getSubCommands(cmd *cobra.Command) []*cobra.Command {
subCommands := []*cobra.Command{}
for _, subCmd := range cmd.Commands() {
subCommands = append(subCommands, subCmd)
subCommands = append(subCommands, getSubCommands(subCmd)...)
}
return subCommands
}
示例11: runImageCatManifest
func runImageCatManifest(cmd *cobra.Command, args []string) (exit int) {
if len(args) != 1 {
cmd.Usage()
return 1
}
s, err := store.NewStore(globalFlags.Dir)
if err != nil {
stderr("image cat-manifest: cannot open store: %v", err)
return 1
}
var key string
if _, err := types.NewHash(args[0]); err == nil {
key, err = s.ResolveKey(args[0])
if err != nil {
stderr("image cat-manifest: cannot resolve key: %v", err)
return 1
}
} else {
app, err := discovery.NewAppFromString(args[0])
if err != nil {
stderr("image cat-manifest: cannot parse the image name: %v", err)
return 1
}
labels, err := types.LabelsFromMap(app.Labels)
if err != nil {
stderr("image cat-manifest: invalid labels in the name: %v", err)
return 1
}
key, err = s.GetACI(app.Name, labels)
if err != nil {
stderr("image cat-manifest: cannot find image: %v", err)
return 1
}
}
manifest, err := s.GetImageManifest(key)
if err != nil {
stderr("image cat-manifest: cannot get image manifest: %v", err)
return 1
}
var b []byte
if flagPrettyPrint {
b, err = json.MarshalIndent(manifest, "", "\t")
} else {
b, err = json.Marshal(manifest)
}
if err != nil {
stderr("image cat-manifest: cannot read the image manifest: %v", err)
return 1
}
stdout(string(b))
return 0
}
示例12: runFetch
func runFetch(cmd *cobra.Command, args []string) (exit int) {
if err := parseApps(&rktApps, args, cmd.Flags(), false); err != nil {
stderr("fetch: unable to parse arguments: %v", err)
return 1
}
if rktApps.Count() < 1 {
stderr("fetch: must provide at least one image")
return 1
}
if flagStoreOnly && flagNoStore {
stderr("both --store-only and --no-store specified")
return 1
}
s, err := store.NewStore(globalFlags.Dir)
if err != nil {
stderr("fetch: cannot open store: %v", err)
return 1
}
ks := getKeystore()
config, err := getConfig()
if err != nil {
stderr("fetch: cannot get configuration: %v", err)
return 1
}
ft := &fetcher{
imageActionData: imageActionData{
s: s,
ks: ks,
headers: config.AuthPerHost,
dockerAuth: config.DockerCredentialsPerRegistry,
insecureSkipVerify: globalFlags.InsecureSkipVerify,
debug: globalFlags.Debug,
},
storeOnly: flagStoreOnly,
noStore: flagNoStore,
withDeps: true,
}
err = rktApps.Walk(func(app *apps.App) error {
hash, err := ft.fetchImage(app.Image, app.Asc)
if err != nil {
return err
}
shortHash := types.ShortHash(hash)
stdout(shortHash)
return nil
})
if err != nil {
stderr("%v", err)
return 1
}
return
}
示例13: runEnter
func runEnter(cmd *cobra.Command, args []string) (exit int) {
if len(args) < 1 {
cmd.Usage()
return 1
}
p, err := getPodFromUUIDString(args[0])
if err != nil {
stderr("Problem problem retrieving pod: %v", err)
return 1
}
defer p.Close()
if !p.isRunning() {
stderr("Pod %q isn't currently running", p.uuid)
return 1
}
podPID, err := p.getContainerPID1()
if err != nil {
stderr("Unable to determine the pid for pod %q: %v", p.uuid, err)
return 1
}
appName, err := getAppName(p)
if err != nil {
stderr("Unable to determine app name: %v", err)
return 1
}
argv, err := getEnterArgv(p, args)
if err != nil {
stderr("Enter failed: %v", err)
return 1
}
s, err := store.NewStore(getDataDir())
if err != nil {
stderr("Cannot open store: %v", err)
return 1
}
stage1TreeStoreID, err := p.getStage1TreeStoreID()
if err != nil {
stderr("Error getting stage1 treeStoreID: %v", err)
return 1
}
stage1RootFS := s.GetTreeStoreRootFS(stage1TreeStoreID)
if err = stage0.Enter(p.path(), podPID, *appName, stage1RootFS, argv); err != nil {
stderr("Enter failed: %v", err)
return 1
}
// not reached when stage0.Enter execs /enter
return 0
}
示例14: runImageExport
func runImageExport(cmd *cobra.Command, args []string) (exit int) {
if len(args) != 2 {
cmd.Usage()
return 1
}
s, err := store.NewStore(globalFlags.Dir)
if err != nil {
stderr("image export: cannot open store: %v", err)
return 1
}
key, err := getKeyFromAppOrHash(s, args[0])
if err != nil {
stderr("image export: %v", err)
return 1
}
aci, err := s.ReadStream(key)
if err != nil {
stderr("image export: error reading image: %v", err)
return 1
}
defer aci.Close()
mode := os.O_CREATE | os.O_WRONLY
if flagOverwriteACI {
mode |= os.O_TRUNC
} else {
mode |= os.O_EXCL
}
f, err := os.OpenFile(args[1], mode, 0644)
if err != nil {
if os.IsExist(err) {
stderr("image export: output ACI file exists (try --overwrite)")
} else {
stderr("image export: unable to open output ACI file %s: %v", args[1], err)
}
return 1
}
defer func() {
err := f.Close()
if err != nil {
stderr("image export: error closing output ACI file: %v", err)
exit = 1
}
}()
_, err = io.Copy(f, aci)
if err != nil {
stderr("image export: error writing to output ACI file: %v", err)
return 1
}
return 0
}
示例15: runRm
func runRm(cmd *cobra.Command, args []string) (exit int) {
var podUUID *types.UUID
var podUUIDs []*types.UUID
var err error
switch {
case len(args) == 0 && flagUUIDFile != "":
podUUID, err = readUUIDFromFile(flagUUIDFile)
if err != nil {
stderr("Unable to read UUID from file: %v", err)
return 1
}
podUUIDs = append(podUUIDs, podUUID)
case len(args) > 0 && flagUUIDFile == "":
for _, uuid := range args {
podUUID, err := resolveUUID(uuid)
if err != nil {
stderr("Unable to resolve UUID: %v", err)
} else {
podUUIDs = append(podUUIDs, podUUID)
}
}
default:
cmd.Usage()
return 1
}
ret := 0
for _, podUUID = range podUUIDs {
p, err := getPod(podUUID)
if err != nil {
ret = 1
stderr("Cannot get pod: %v", err)
}
if removePod(p) {
stdout("%q", p.uuid)
} else {
ret = 1
}
}
if ret == 1 {
stderr("Failed to remove one or more pods")
}
return ret
}