forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (94 loc) · 3.18 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"os"
"github.com/heroku/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/buildpacks/pack"
"github.com/buildpacks/pack/cmd"
"github.com/buildpacks/pack/internal/commands"
"github.com/buildpacks/pack/internal/config"
clilogger "github.com/buildpacks/pack/internal/logging"
"github.com/buildpacks/pack/logging"
)
var packClient pack.Client
func main() {
// create logger with defaults
logger := clilogger.NewLogWithWriters(color.Stdout(), color.Stderr())
cobra.EnableCommandSorting = false
cfg, err := initConfig()
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
rootCmd := &cobra.Command{
Use: "pack",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if fs := cmd.Flags(); fs != nil {
if flag, err := fs.GetBool("no-color"); err == nil {
color.Disable(flag)
}
if flag, err := fs.GetBool("quiet"); err == nil {
logger.WantQuiet(flag)
}
if flag, err := fs.GetBool("verbose"); err == nil {
logger.WantVerbose(flag)
}
if flag, err := fs.GetBool("timestamps"); err == nil {
logger.WantTime(flag)
}
}
packClient = initClient(logger)
},
}
rootCmd.PersistentFlags().Bool("no-color", false, "Disable color output")
rootCmd.PersistentFlags().Bool("timestamps", false, "Enable timestamps in output")
rootCmd.PersistentFlags().BoolP("quiet", "q", false, "Show less output")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Show more output")
rootCmd.Flags().Bool("version", false, "Show current 'pack' version")
commands.AddHelpFlag(rootCmd, "pack")
rootCmd.AddCommand(commands.Build(logger, cfg, &packClient))
rootCmd.AddCommand(commands.Run(logger, cfg, &packClient))
rootCmd.AddCommand(commands.Rebase(logger, cfg, &packClient))
rootCmd.AddCommand(commands.InspectImage(logger, &cfg, &packClient))
rootCmd.AddCommand(commands.CreateBuilder(logger, &packClient))
rootCmd.AddCommand(commands.CreatePackage(logger, &packClient))
rootCmd.AddCommand(commands.SetRunImagesMirrors(logger, cfg))
rootCmd.AddCommand(commands.InspectBuilder(logger, cfg, &packClient))
rootCmd.AddCommand(commands.SetDefaultBuilder(logger, cfg, &packClient))
rootCmd.AddCommand(commands.SuggestBuilders(logger, &packClient))
rootCmd.AddCommand(commands.SuggestStacks(logger))
rootCmd.AddCommand(commands.Version(logger, cmd.Version))
rootCmd.AddCommand(commands.Report(logger))
rootCmd.AddCommand(commands.CompletionCommand(logger))
rootCmd.Version = cmd.Version
rootCmd.SetVersionTemplate(`{{.Version}}{{"\n"}}`)
if err := rootCmd.Execute(); err != nil {
if commands.IsSoftError(err) {
os.Exit(2)
}
os.Exit(1)
}
}
func initConfig() (config.Config, error) {
path, err := config.DefaultConfigPath()
if err != nil {
return config.Config{}, errors.Wrap(err, "getting config path")
}
cfg, err := config.Read(path)
if err != nil {
return config.Config{}, errors.Wrap(err, "reading pack config")
}
return cfg, nil
}
func initClient(logger logging.Logger) pack.Client {
client, err := pack.NewClient(pack.WithLogger(logger))
if err != nil {
exitError(logger, err)
}
return *client
}
func exitError(logger logging.Logger, err error) {
logger.Error(err.Error())
os.Exit(1)
}