From 01024076068834908da9f3575537a8fa437ffba9 Mon Sep 17 00:00:00 2001 From: Stefan Orosz Date: Sun, 11 Sep 2022 15:31:16 +0200 Subject: [PATCH] [NEW] selfupdate --- cmd/list.go | 21 +++++++----- cmd/root.go | 3 +- cmd/selfupdate.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 cmd/selfupdate.go diff --git a/cmd/list.go b/cmd/list.go index 836d63e..c124714 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -15,14 +15,10 @@ import ( // listCmd represents the list command var listCmd = &cobra.Command{ - Use: "list", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, + Use: "list ", + Short: "List bucket contents", + Long: `This command lists the content of specified bucket`, + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { isps3 := util.S3{ Endpoint: "isp-dev.paasdev.sk:9000", @@ -36,7 +32,14 @@ to quickly create a Cobra application.`, } ctx := context.Background() - objectCh := isps3.Client.ListObjects(ctx, "test", minio.ListObjectsOptions{ + found, err := isps3.Client.BucketExists(ctx, args[0]) + if err != nil { + return err + } + if !found { + return fmt.Errorf("bucket %q does not exist", args[0]) + } + objectCh := isps3.Client.ListObjects(ctx, args[0], minio.ListObjectsOptions{ Prefix: "", Recursive: true, }) diff --git a/cmd/root.go b/cmd/root.go index 76da54d..b9130c1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,7 +22,8 @@ to quickly create a Cobra application.`, // Uncomment the following line if your bare application // has an action associated with it: // Run: func(cmd *cobra.Command, args []string) { }, - Version: "0.0.1", + Version: "0.0.5", + SilenceUsage: true, } // Execute adds all child commands to the root command and sets flags appropriately. diff --git a/cmd/selfupdate.go b/cmd/selfupdate.go new file mode 100644 index 0000000..30105d2 --- /dev/null +++ b/cmd/selfupdate.go @@ -0,0 +1,86 @@ +/* +Copyright © 2022 NAME HERE +*/ +package cmd + +import ( + "context" + "dummy/util" + "fmt" + "runtime" + + "github.com/minio/minio-go/v7" + "github.com/minio/selfupdate" + "github.com/spf13/cobra" +) + +var ( + Ver string + List bool +) + +// selfupdateCmd represents the selfupdate command +var selfupdateCmd = &cobra.Command{ + Use: "selfupdate", + Short: "Updates to latest or specific version", + Long: `Updates to latest or specific version of dummy. + +To update to latest version use: + dummy selfupdate + +To update to version 0.0.3 use: + dummy selfupdate --version=0.0.3`, + RunE: func(cmd *cobra.Command, args []string) error { + + // Initialize minio client + isps3 := util.S3{ + Endpoint: "isp-dev.paasdev.sk:9000", + AccessKeyID: "LHHV6WR7EEMRS1RLX6BW", + SecretAccessKey: "m8PvnzpRB4ORYwcJtzeJpboFUIBAfbZ3wwh3Bkcp", + UseSSL: true, + } + err := isps3.Init() + if err != nil { + return err + } + + if List { + updateCol := isps3.Client.ListObjects(context.Background(), "dummy", minio.ListObjectsOptions{ + Prefix: fmt.Sprintf("%s/dummy", runtime.GOOS), + }) + + fmt.Println("Available updates:") + for object := range updateCol { + if object.Err != nil { + return err + } + fmt.Println(object.Key) + } + return nil + } + + path := fmt.Sprintf("%s/dummy-%s", runtime.GOOS, Ver) + fmt.Println("Updating dummy to: ", path) + // Read the update + update, err := isps3.Client.GetObject(context.Background(), "dummy", path, minio.GetObjectOptions{}) + if err != nil { + return err + } + // Do update + err = selfupdate.Apply(update, selfupdate.Options{}) + if err != nil { + return err + } + fmt.Println("Update finished") + return nil + }, +} + +func init() { + rootCmd.AddCommand(selfupdateCmd) + + selfupdateCmd.Flags().StringVar(&Ver, "version", "latest", "Version to install (optional)") + selfupdateCmd.Flags().BoolVar(&List, "list", false, "List available versions (optional)") + + selfupdateCmd.MarkFlagsMutuallyExclusive("version", "list") +}