-
Notifications
You must be signed in to change notification settings - Fork 56
/
export.go
73 lines (61 loc) · 2 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package upgradecmd
import (
"os"
"github.com/ava-labs/avalanche-cli/pkg/constants"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/spf13/cobra"
)
var force bool
// avalanche subnet upgrade import
func newUpgradeExportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export [subnetName]",
Short: "Export the upgrade bytes file to a location of choice on disk",
Long: `Export the upgrade bytes file to a location of choice on disk`,
RunE: upgradeExportCmd,
Args: cobra.ExactArgs(1),
}
cmd.Flags().StringVar(&upgradeBytesFilePath, upgradeBytesFilePathKey, "", "Export upgrade bytes file to location of choice on disk")
cmd.Flags().BoolVar(&force, "force", false, "If true, overwrite a possibly existing file without prompting")
return cmd
}
func upgradeExportCmd(_ *cobra.Command, args []string) error {
subnetName := args[0]
if !app.GenesisExists(subnetName) {
ux.Logger.PrintToUser("The provided subnet name %q does not exist", subnetName)
return nil
}
if upgradeBytesFilePath == "" {
var err error
upgradeBytesFilePath, err = app.Prompt.CaptureString("Provide a path where we should export the file to")
if err != nil {
return err
}
}
if !force {
if _, err := os.Stat(upgradeBytesFilePath); err == nil {
ux.Logger.PrintToUser("The file specified with path %q already exists!", upgradeBytesFilePath)
yes, err := app.Prompt.CaptureYesNo("Should we overwrite it?")
if err != nil {
return err
}
if !yes {
ux.Logger.PrintToUser("Aborted by user. Nothing has been exported")
return nil
}
}
}
fileBytes, err := app.ReadUpgradeFile(subnetName)
if err != nil {
return err
}
ux.Logger.PrintToUser("Writing the upgrade bytes file to %q...", upgradeBytesFilePath)
err = os.WriteFile(upgradeBytesFilePath, fileBytes, constants.DefaultPerms755)
if err != nil {
return err
}
ux.Logger.PrintToUser("File written successfully.")
return nil
}