/* 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") }