Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service/cmd/root.go
2498 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
"os"
11
12
"github.com/gitpod-io/gitpod/common-go/log"
13
"github.com/gitpod-io/gitpod/common-go/tracing"
14
"github.com/gitpod-io/gitpod/content-service/api/config"
15
"github.com/spf13/cobra"
16
)
17
18
var (
19
// ServiceName is the name we use for tracing/logging
20
ServiceName = "content-service"
21
// Version of this service - set during build
22
Version = ""
23
)
24
25
var jsonLog bool
26
var verbose bool
27
var configFile string
28
29
var rootCmd = &cobra.Command{
30
Use: "content-service",
31
Short: "Content service",
32
PersistentPreRun: func(cmd *cobra.Command, args []string) {
33
log.Init(ServiceName, Version, jsonLog, verbose)
34
},
35
}
36
37
// Execute runs this main command
38
func Execute() {
39
closer := tracing.Init(ServiceName)
40
if closer != nil {
41
defer closer.Close()
42
}
43
44
if err := rootCmd.Execute(); err != nil {
45
fmt.Println(err)
46
os.Exit(1)
47
}
48
}
49
50
func getConfig() *config.ServiceConfig {
51
ctnt, err := os.ReadFile(configFile)
52
if err != nil {
53
log.WithError(fmt.Errorf("cannot read config: %w", err)).Error("cannot read configuration. Maybe missing --config?")
54
os.Exit(1)
55
}
56
57
var cfg config.ServiceConfig
58
err = json.Unmarshal(ctnt, &cfg)
59
if err != nil {
60
log.WithError(err).Error("cannot read configuration. Maybe missing --config?")
61
os.Exit(1)
62
}
63
64
return &cfg
65
}
66
67
func init() {
68
rootCmd.PersistentFlags().BoolVarP(&jsonLog, "json-log", "j", true, "produce JSON log output on verbose level")
69
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose JSON logging")
70
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file")
71
}
72
73