This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
115 lines (94 loc) · 3.41 KB
/
root.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
115
/*
Copyright © 2019 Azim Sonawalla <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"log"
"os"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "unbake",
Short: "Convert docker buildx bake files into sequential docker commands",
Long: `docker/buildx is a plugin to the most recent docker distribution
that allows users to define high level build information (bake files)
and transmit that build info to the buildkit-powered docker daemon
in a single invocation.
Since this is a relatively new piece of technolgoy, many environments (including
primarily CI and CD systems) don't yet have the most recent version of docker or
the buildx plugin, and it introduces significant overhead to install these.
unbake solves for this discrepancy by taking a bake file as an input and generating
plain docker commands to build those targets. You'd want to run the unbake container
in your CI against a bake file and pipe the output of it to shell to continue with a
multi-invocation docker build process.`,
Run: func(cmd *cobra.Command, args []string) {
for _, file := range files {
var commands, err = unbake(file)
if err != nil {
log.Panic(err)
}
for _, command := range commands {
fmt.Println(command)
}
}
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
var files []string
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "",
"config file (default is $HOME/.unbake.yaml)")
rootCmd.Flags().StringArrayVarP(&files, "file", "f", []string{},
"bake file to process")
_ = rootCmd.MarkFlagRequired("file")
rootCmd.Flags().BoolVarP(&buildKit, "buildkit", "b", false,
"prepend DOCKER_BUILDKIT=1 to commands")
rootCmd.Flags().StringVar(&dockerCfg, "docker-config", "",
"custom docker client config directory")
rootCmd.Flags().BoolVarP(&quiet, "quiet", "q", false,
"docker build only prints results")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".unbake" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".unbake")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}