Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/image-builder-mk3/cmd/root.go
2497 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"encoding/json"
9
"fmt"
10
"github.com/gitpod-io/gitpod/image-builder/api/config"
11
"os"
12
13
"github.com/mattn/go-isatty"
14
"github.com/spf13/cobra"
15
"golang.org/x/xerrors"
16
17
"github.com/gitpod-io/gitpod/common-go/log"
18
"github.com/gitpod-io/gitpod/common-go/tracing"
19
)
20
21
var (
22
// ServiceName is the name we use for tracing/logging
23
ServiceName = "image-builder-mk3"
24
// Version of this service - set during build
25
Version = ""
26
)
27
28
var jsonLog bool
29
var verbose bool
30
var configFile string
31
var rootCmd = &cobra.Command{
32
Use: "image-builder-mk3",
33
Short: "Workspace image-builder service",
34
PersistentPreRun: func(cmd *cobra.Command, args []string) {
35
log.Init(ServiceName, Version, jsonLog && !isatty.IsTerminal(os.Stdout.Fd()), verbose)
36
},
37
}
38
39
// Execute runs this main command
40
func Execute() {
41
closer := tracing.Init(ServiceName)
42
if closer != nil {
43
defer closer.Close()
44
}
45
46
if err := rootCmd.Execute(); err != nil {
47
fmt.Println(err)
48
os.Exit(1)
49
}
50
}
51
52
func getConfig() *config.ServiceConfig {
53
ctnt, err := os.ReadFile(configFile)
54
if err != nil {
55
log.WithError(xerrors.Errorf("cannot read config: %w", err)).Error("cannot read configuration. Maybe missing --config?")
56
os.Exit(1)
57
}
58
59
var cfg config.ServiceConfig
60
err = json.Unmarshal(ctnt, &cfg)
61
if err != nil {
62
log.WithError(err).Error("cannot read configuration. Maybe missing --config?")
63
os.Exit(1)
64
}
65
66
return &cfg
67
}
68
69
func init() {
70
rootCmd.PersistentFlags().BoolVarP(&jsonLog, "json-log", "j", true, "produce JSON log output on verbose level")
71
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose JSON logging")
72
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file")
73
}
74
75