Skip to content

Commit

Permalink
Add files for kando, a command line tool (#3158)
Browse files Browse the repository at this point in the history
* Add kantools files

* Review feedback

* Rename ObjectStore command to Location.

* Rename suffix to path
  • Loading branch information
tdmanv committed Jun 15, 2018
1 parent 7000a05 commit 58f2874
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/kando/main.go
@@ -0,0 +1,9 @@
package main

import (
"github.com/kanisterio/kanister/pkg/kando"
)

func main() {
kando.Execute()
}
28 changes: 28 additions & 0 deletions pkg/kando/kando.go
@@ -0,0 +1,28 @@
package kando

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

kanister "github.com/kanisterio/kanister/pkg"
)

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
root := newRootCommand()
if err := root.Execute(); err != nil {
log.Errorf("%+v", err)
}
}

func newRootCommand() *cobra.Command {
// RootCmd represents the base command when called without any subcommands
rootCmd := &cobra.Command{
Use: "kando <command>",
Short: "A set of tools used from Kanister Blueprints",
Version: kanister.VERSION,
}
rootCmd.AddCommand(newLocationCommand())
return rootCmd
}
39 changes: 39 additions & 0 deletions pkg/kando/location.go
@@ -0,0 +1,39 @@
package kando

import (
"encoding/json"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/kanisterio/kanister/pkg/param"
)

const (
pathFlagName = "path"
profileFlagName = "profile"
)

func newLocationCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "location <command>",
Short: "Push and pull from object storage",
}
cmd.AddCommand(newLocationPushCommand())
cmd.AddCommand(newLocationPullCommand())
cmd.PersistentFlags().StringP(pathFlagName, "s", "", "Specify a path suffix (optional)")
cmd.PersistentFlags().StringP(profileFlagName, "p", "", "Pass a Profile as a JSON string (required)")
cmd.MarkFlagRequired(profileFlagName)
return cmd
}

func pathFlag(cmd *cobra.Command) string {
return cmd.Flag(pathFlagName).Value.String()
}

func unmarshalProfileFlag(cmd *cobra.Command) (*param.Profile, error) {
profileJSON := cmd.Flag(profileFlagName).Value.String()
p := &param.Profile{}
err := json.Unmarshal([]byte(profileJSON), p)
return p, errors.Wrap(err, "failed to unmarshal profile")
}
39 changes: 39 additions & 0 deletions pkg/kando/location_pull.go
@@ -0,0 +1,39 @@
package kando

import (
"context"

"github.com/spf13/cobra"

"github.com/kanisterio/kanister/pkg/param"
)

func newLocationPullCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "pull <target>",
Short: "Pull from s3-compliant object storage to a file or stdout",
Args: cobra.ExactArgs(1),
// TODO: Example invocations
RunE: func(c *cobra.Command, args []string) error {
return runLocationPull(c, args)
},
}
return cmd

}

func runLocationPull(cmd *cobra.Command, args []string) error {
source := args[0]
p, err := unmarshalProfileFlag(cmd)
if err != nil {
return err
}
s := pathFlag(cmd)
ctx := context.Background()
return locationPull(ctx, p, s, source)
}

// TODO: Implement this function
func locationPull(ctx context.Context, p *param.Profile, path string, source string) error {
return nil
}
39 changes: 39 additions & 0 deletions pkg/kando/location_push.go
@@ -0,0 +1,39 @@
package kando

import (
"context"

"github.com/spf13/cobra"

"github.com/kanisterio/kanister/pkg/param"
)

func newLocationPushCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "push <source>",
Short: "Push a source file or stdin stream to s3-compliant object storage",
Args: cobra.ExactArgs(1),
// TODO: Example invocations
RunE: func(c *cobra.Command, args []string) error {
return runLocationPush(c, args)
},
}
return cmd

}

func runLocationPush(cmd *cobra.Command, args []string) error {
source := args[0]
p, err := unmarshalProfileFlag(cmd)
if err != nil {
return err
}
s := pathFlag(cmd)
ctx := context.Background()
return locationPush(ctx, p, s, source)
}

// TODO: Implement this function
func locationPush(ctx context.Context, p *param.Profile, path string, source string) error {
return nil
}

0 comments on commit 58f2874

Please sign in to comment.