|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/gobitfly/beaconchain/pkg/commons/db" |
| 9 | + "github.com/gobitfly/beaconchain/pkg/commons/log" |
| 10 | + |
| 11 | + "github.com/pkg/errors" |
| 12 | +) |
| 13 | + |
| 14 | +type AppBundleCommand struct { |
| 15 | + FlagSet *flag.FlagSet |
| 16 | + Config appBundleCommandConfig |
| 17 | +} |
| 18 | + |
| 19 | +type appBundleCommandConfig struct { |
| 20 | + DryRun bool |
| 21 | + Force bool // bypass summary confirm |
| 22 | + BundleURL string |
| 23 | + BundleVersionCode int64 |
| 24 | + NativeVersionCode int64 |
| 25 | + TargetInstalls int64 |
| 26 | +} |
| 27 | + |
| 28 | +func (s *AppBundleCommand) ParseCommandOptions() { |
| 29 | + s.FlagSet.Int64Var(&s.Config.BundleVersionCode, "version-code", 0, "Version code of that bundle (Default: Next)") |
| 30 | + s.FlagSet.Int64Var(&s.Config.NativeVersionCode, "min-native-version", 0, "Minimum required native version (Default: Current)") |
| 31 | + s.FlagSet.Int64Var(&s.Config.TargetInstalls, "target-installs", -1, "How many people to roll out to (Default: All)") |
| 32 | + s.FlagSet.StringVar(&s.Config.BundleURL, "bundle-url", "", "URL to bundle that contains the update, bundle.zip") |
| 33 | + s.FlagSet.BoolVar(&s.Config.Force, "force", false, "Skips summary and confirmation") |
| 34 | +} |
| 35 | + |
| 36 | +func (s *AppBundleCommand) Run() error { |
| 37 | + if s.Config.BundleURL == "" { |
| 38 | + s.showHelp() |
| 39 | + return errors.New("Please provide a valid bundle URL via --bundle-url") |
| 40 | + } |
| 41 | + if s.Config.BundleVersionCode == 0 { |
| 42 | + fileName := strings.Split(s.Config.BundleURL, "/") |
| 43 | + if len(fileName) == 0 { |
| 44 | + return errors.New("Invalid bundle URL") |
| 45 | + } |
| 46 | + |
| 47 | + split := strings.Split(fileName[len(fileName)-1], "_") |
| 48 | + if len(split) < 2 { |
| 49 | + return errors.New("Invalid bundle URL") |
| 50 | + } |
| 51 | + |
| 52 | + // split[1] is the version code |
| 53 | + _, err := fmt.Sscanf(split[1], "%d", &s.Config.BundleVersionCode) |
| 54 | + if err != nil { |
| 55 | + return errors.Wrap(err, "Error parsing version code") |
| 56 | + } |
| 57 | + } |
| 58 | + if s.Config.NativeVersionCode <= 0 { |
| 59 | + err := db.ReaderDb.Get(&s.Config.NativeVersionCode, "SELECT MAX(min_native_version) FROM mobile_app_bundles") |
| 60 | + if err != nil { |
| 61 | + return errors.Wrap(err, "Error getting max native version") |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if s.Config.TargetInstalls < 0 { |
| 66 | + s.Config.TargetInstalls = -1 |
| 67 | + } |
| 68 | + |
| 69 | + if !s.Config.Force { |
| 70 | + // Summary |
| 71 | + log.Infof("=== Bundle Summary ===") |
| 72 | + log.Infof("Bundle URL: %s", s.Config.BundleURL) |
| 73 | + log.Infof("Bundle Version Code: %d", s.Config.BundleVersionCode) |
| 74 | + log.Infof("Minimum Native Version: %d", s.Config.NativeVersionCode) |
| 75 | + if s.Config.TargetInstalls == -1 { |
| 76 | + log.Infof("Target Installs: All") |
| 77 | + } else { |
| 78 | + log.Infof("Target Installs: %d", s.Config.TargetInstalls) |
| 79 | + } |
| 80 | + log.Infof("======================\n") |
| 81 | + |
| 82 | + // ask for y/n input |
| 83 | + log.Infof("Do you want to add this bundle? (y/n)\n") |
| 84 | + var input string |
| 85 | + _, err := fmt.Scanln(&input) |
| 86 | + if err != nil { |
| 87 | + return errors.Wrap(err, "Error reading input") |
| 88 | + } |
| 89 | + |
| 90 | + if input != "y" { |
| 91 | + log.Infof("Bundle not added\n") |
| 92 | + return nil |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if s.Config.DryRun { |
| 97 | + log.Infof("Dry run, not adding bundle\n") |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + _, err := db.WriterDb.Exec("INSERT INTO mobile_app_bundles (bundle_url, bundle_version, min_native_version, target_count) VALUES ($1, $2, $3, $4)", s.Config.BundleURL, s.Config.BundleVersionCode, s.Config.NativeVersionCode, s.Config.TargetInstalls) |
| 102 | + if err != nil { |
| 103 | + return errors.Wrap(err, "Error inserting app bundle") |
| 104 | + } |
| 105 | + |
| 106 | + log.Infof("Bundle added successfully") |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func (s *AppBundleCommand) showHelp() { |
| 111 | + log.Infof("Usage: app_bundle [options]") |
| 112 | + log.Infof("Options:") |
| 113 | + log.Infof(" --version-code int\tVersion code of that bundle") |
| 114 | + log.Infof(" --min-native-version int\tMinimum required native version (Default: Current)") |
| 115 | + log.Infof(" --target-installs int\tHow many people to roll out to (Default: All)") |
| 116 | + log.Infof(" --bundle-url string\tURL to bundle that contains the update, bundle.zip") |
| 117 | +} |
0 commit comments