Skip to content

Commit

Permalink
test: add demo command
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Jul 10, 2024
1 parent d06bb81 commit adc2d50
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/oras/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func New() *cobra.Command {
blob.Cmd(),
manifest.Cmd(),
repo.Cmd(),
demoCmd(),
)
return cmd
}
67 changes: 67 additions & 0 deletions cmd/oras/root/demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package root

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

type demoOptions struct {
pairs map[string]string
slice []string
tuple []string
}

func demoCmd() *cobra.Command {
var demoOptions demoOptions
cmd := &cobra.Command{
Use: "demo",
Short: "Demo of flags",
Args: func(cmd *cobra.Command, args []string) error {
for i, arg := range args {
fmt.Fprintf(os.Stderr, "[%d]: %s\n", i, arg)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return runDemo(&demoOptions)
},
}

cmd.Flags().StringToStringVarP(&demoOptions.pairs, "paired", "p", nil, "paired key-value pairs")
cmd.Flags().StringSliceVarP(&demoOptions.slice, "slice", "s", []string{}, "Add a slice of values")
cmd.Flags().StringArrayVarP(&demoOptions.tuple, "tuple", "t", []string{}, "Add a tuple of values")
return cmd
}

func runDemo(o *demoOptions) error {
fmt.Println("Key-value pairs:")
for k, v := range o.pairs {
fmt.Printf("> %s: %s\n", k, v)
}
fmt.Println("Slices:")
for i, t := range o.slice {
fmt.Printf("> [%d]: %s\n", i, t)
}
fmt.Println("Tuples:")
for i, t := range o.tuple {
fmt.Printf("> [%d]: %s\n", i, t)
}
return nil
}

0 comments on commit adc2d50

Please sign in to comment.