Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/cmd/root.go
2498 views
1
// Copyright (c) 2022 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
"bytes"
9
"context"
10
"encoding/json"
11
"os"
12
"path"
13
14
"github.com/gitpod-io/gitpod/components/public-api/go/config"
15
16
"github.com/gitpod-io/gitpod/common-go/log"
17
"github.com/spf13/cobra"
18
)
19
20
var (
21
// ServiceName is the name we use for tracing/logging
22
ServiceName = "public-api-server"
23
// Version of this service - set during build
24
Version = ""
25
)
26
27
var rootOpts struct {
28
CfgFile string
29
JsonLog bool
30
Verbose bool
31
}
32
33
// rootCmd represents the base command when called without any subcommands
34
var rootCmd = &cobra.Command{
35
Use: ServiceName,
36
Short: "Serves public API services",
37
PersistentPreRun: func(cmd *cobra.Command, args []string) {
38
log.Init(ServiceName, Version, rootOpts.JsonLog, rootOpts.Verbose)
39
},
40
}
41
42
func Execute() {
43
if err := rootCmd.ExecuteContext(context.Background()); err != nil {
44
log.WithError(err).Error("Failed to execute command.")
45
os.Exit(1)
46
}
47
}
48
49
func init() {
50
localConfig := path.Join(os.ExpandEnv("GOMOD"), "..", "config.json")
51
rootCmd.PersistentFlags().StringVar(&rootOpts.CfgFile, "config", localConfig, "config file")
52
rootCmd.PersistentFlags().BoolVar(&rootOpts.JsonLog, "json-log", true, "produce JSON log output on verbose level")
53
rootCmd.PersistentFlags().BoolVar(&rootOpts.Verbose, "verbose", false, "Enable verbose JSON logging")
54
}
55
56
func getConfig() *config.Configuration {
57
ctnt, err := os.ReadFile(rootOpts.CfgFile)
58
if err != nil {
59
log.WithError(err).Fatal("Cannot read configuration. Maybe missing --config?")
60
}
61
62
var cfg config.Configuration
63
dec := json.NewDecoder(bytes.NewReader(ctnt))
64
dec.DisallowUnknownFields()
65
err = dec.Decode(&cfg)
66
if err != nil {
67
log.WithError(err).Fatal("Cannot decode configuration. Maybe missing --config?")
68
}
69
70
return &cfg
71
}
72
73