fix error handling

This commit is contained in:
2023-03-29 14:59:04 +02:00
parent 0e572f0aa4
commit 50d25fe7b7
14 changed files with 96 additions and 48 deletions

View File

@@ -33,8 +33,7 @@ func FormCommand(conf *cfg.Config) *cobra.Command {
// errors at this stage do not cause the usage to be shown
//cmd.SilenceUsage = true
if len(args) == 0 {
cmd.Help()
os.Exit(0)
return cmd.Help()
}
return nil
},

View File

@@ -130,13 +130,12 @@ func initConfig(cmd *cobra.Command, cfg *cfg.Config) error {
v.SetEnvPrefix("upctl")
// map flags to viper
bindFlags(cmd, v)
return nil
return bindFlags(cmd, v)
}
// bind flags to viper settings (env+cfgfile)
func bindFlags(cmd *cobra.Command, v *viper.Viper) {
func bindFlags(cmd *cobra.Command, v *viper.Viper) error {
var fail error
cmd.Flags().VisitAll(func(f *pflag.Flag) {
// map flag name to config variable
configName := f.Name
@@ -144,7 +143,11 @@ func bindFlags(cmd *cobra.Command, v *viper.Viper) {
// use config variable if flag is not set and config is set
if !f.Changed && v.IsSet(configName) {
val := v.Get(configName)
cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil {
fail = err
}
}
})
return fail
}

View File

@@ -181,7 +181,9 @@ func UploadFiles(w io.Writer, c *cfg.Config, args []string) error {
var left float64
rq.R.SetUploadCallbackWithInterval(func(info req.UploadInfo) {
left = float64(info.UploadedSize) / float64(info.FileSize) * 100.0
bar.Add(int(left))
if err := bar.Add(int(left)); err != nil {
fmt.Print("\r")
}
}, 10*time.Millisecond)
}
@@ -278,7 +280,9 @@ func Download(w io.Writer, c *cfg.Config, args []string) error {
callback := func(info req.DownloadInfo) {
if info.Response.Response != nil {
bar.Add(1)
if err := bar.Add(1); err != nil {
fmt.Print("\r")
}
}
}
@@ -344,6 +348,4 @@ func CreateForm(w io.Writer, c *cfg.Config) error {
}
return RespondExtended(w, resp)
return nil
}

View File

@@ -38,8 +38,6 @@ func prepareExpire(expire string, start common.Timestamp) string {
return time.Unix(start.Unix()+int64(common.Duration2int(expire)), 0).
Format("2006-01-02 15:04:05")
}
return ""
}
// generic table writer

View File

@@ -1 +0,0 @@
Mon Mar 20 12:16:26 PM CET 2023