Skip to content

Commit

Permalink
cmd: add "s3/sts" utils
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
  • Loading branch information
gyuho committed Jul 13, 2020
1 parent a8c26a4 commit 3ebee36
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 43 deletions.
36 changes: 0 additions & 36 deletions cmd/aws-utils/main.go

This file was deleted.

77 changes: 77 additions & 0 deletions cmd/s3-utils/cp/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Package cp implements "aws s3 cp" commands.
package cp

import (
"fmt"
"os"

pkg_aws "github.com/aws/aws-k8s-tester/pkg/aws"
pkg_s3 "github.com/aws/aws-k8s-tester/pkg/aws/s3"
"github.com/aws/aws-k8s-tester/pkg/logutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

var (
logLevel string
partition string
region string
s3Bucket string
s3Key string
localPath string
)

func init() {
cobra.EnablePrefixMatching = true
}

// NewCommand implements "s3-utils cp" command.
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "cp",
Short: "AWS s3 cp commands",
Run: cpFunc,
}
cmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error, dpanic, panic, fatal)")
cmd.PersistentFlags().StringVar(&partition, "partition", "aws", "AWS partition")
cmd.PersistentFlags().StringVar(&region, "region", "us-west-2", "AWS region")
cmd.PersistentFlags().StringVar(&s3Bucket, "s3-bucket", "", "s3 bucket")
cmd.PersistentFlags().StringVar(&s3Key, "s3-key", "", "s3 key")
cmd.PersistentFlags().StringVar(&localPath, "local-path", "", "local download path")
return cmd
}

func cpFunc(cmd *cobra.Command, args []string) {
lcfg := logutil.GetDefaultZapLoggerConfig()
lcfg.Level = zap.NewAtomicLevelAt(logutil.ConvertToZapLevel(logLevel))
lg, err := lcfg.Build()
if err != nil {
panic(err)
}
ss, stsOutput, _, err := pkg_aws.New(&pkg_aws.Config{
Logger: lg,
DebugAPICalls: logLevel == "debug",
Partition: partition,
Region: region,
})
if stsOutput == nil || err != nil {
lg.Warn("failed to create AWS session and get sts caller identity", zap.Error(err))
} else {
roleARN := aws.StringValue(stsOutput.Arn)
fmt.Fprintf(os.Stderr, "\nAccount: %q\n", aws.StringValue(stsOutput.Account))
fmt.Fprintf(os.Stderr, "Role Arn: %q\n", roleARN)
fmt.Fprintf(os.Stderr, "UserId: %q\n\n", aws.StringValue(stsOutput.UserId))
}

if err = pkg_s3.Download(lg, s3.New(ss), s3Bucket, s3Key, localPath, pkg_s3.WithOverwrite(true)); err != nil {
lg.Fatal("failed to download S3 file",
zap.String("s3-bucket", s3Bucket),
zap.String("s3-key", s3Key),
zap.Error(err),
)
} else {
fmt.Fprintf(os.Stderr, "SUCCESSFULLY DOWNLOADED %q %q to %q\n", s3Bucket, s3Key, localPath)
}
}
35 changes: 35 additions & 0 deletions cmd/s3-utils/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// s3-utils is a set of AWS utilities commands.
package main

import (
"fmt"
"os"

"github.com/aws/aws-k8s-tester/cmd/s3-utils/cp"
"github.com/aws/aws-k8s-tester/cmd/s3-utils/version"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "s3-utils",
Short: "AWS utils CLI",
}

func init() {
cobra.EnablePrefixMatching = true
}

func init() {
rootCmd.AddCommand(
cp.NewCommand(),
version.NewCommand(),
)
}

func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "s3-utils failed %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package sts implements "aws sts get-caller-identity" commands.
package sts
// Package getcalleridentity implements "aws sts get-caller-identity" commands.
package getcalleridentity

import (
"fmt"
Expand All @@ -25,12 +25,12 @@ func init() {
cobra.EnablePrefixMatching = true
}

// NewCommand implements "aws-utils sts" command.
// NewCommand implements "sts-utils get-caller-identity" command.
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "sts [OUTPUT-PATH]",
Short: "AWS sts commands",
Run: stsFunc,
Use: "get-caller-identity",
Short: "AWS sts get-caller-identity commands",
Run: getCallerIdentityFunc,
}
cmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error, dpanic, panic, fatal)")
cmd.PersistentFlags().StringVar(&partition, "partition", "aws", "AWS partition")
Expand All @@ -40,7 +40,7 @@ func NewCommand() *cobra.Command {
return cmd
}

func stsFunc(cmd *cobra.Command, args []string) {
func getCallerIdentityFunc(cmd *cobra.Command, args []string) {
lcfg := logutil.GetDefaultZapLoggerConfig()
lcfg.Level = zap.NewAtomicLevelAt(logutil.ConvertToZapLevel(logLevel))
lg, err := lcfg.Build()
Expand Down
36 changes: 36 additions & 0 deletions cmd/sts-utils/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// sts-utils is a set of AWS utilities commands.
package main

import (
"fmt"
"os"

get_caller_identity "github.com/aws/aws-k8s-tester/cmd/sts-utils/get-caller-identity"
"github.com/aws/aws-k8s-tester/cmd/sts-utils/version"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "sts-utils",
Short: "AWS utils CLI",
SuggestFor: []string{"stsutils"},
}

func init() {
cobra.EnablePrefixMatching = true
}

func init() {
rootCmd.AddCommand(
get_caller_identity.NewCommand(),
version.NewCommand(),
)
}

func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "sts-utils failed %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
26 changes: 26 additions & 0 deletions cmd/sts-utils/version/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Package version implements version command.
package version

import (
"fmt"

"github.com/aws/aws-k8s-tester/version"
"github.com/spf13/cobra"
)

func init() {
cobra.EnablePrefixMatching = true
}

// NewCommand implements "cw-utils version" command.
func NewCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Prints out cw-utils version",
Run: versionFunc,
}
}

func versionFunc(cmd *cobra.Command, args []string) {
fmt.Println(version.Version())
}

0 comments on commit 3ebee36

Please sign in to comment.