Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/usage/cmd/run.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
"encoding/json"
10
"fmt"
11
"github.com/gitpod-io/gitpod/common-go/log"
12
"github.com/gitpod-io/gitpod/usage/pkg/server"
13
"github.com/spf13/cobra"
14
"os"
15
"path"
16
)
17
18
func init() {
19
rootCmd.AddCommand(run())
20
}
21
22
func run() *cobra.Command {
23
var (
24
verbose bool
25
configPath string
26
)
27
28
cmd := &cobra.Command{
29
Use: "run",
30
Short: "Starts the service",
31
Version: Version,
32
Run: func(cmd *cobra.Command, args []string) {
33
log.Init(ServiceName, Version, true, verbose)
34
35
cfg, err := parseConfig(configPath)
36
if err != nil {
37
log.WithError(err).Fatal("Failed to get config. Did you specify --config correctly?")
38
}
39
40
err = server.Start(cfg, Version)
41
if err != nil {
42
log.WithError(err).Fatal("Failed to start usage server.")
43
}
44
},
45
}
46
47
localConfig := path.Join(os.ExpandEnv("GOMOD"), "..", "config.json")
48
49
cmd.Flags().BoolVar(&verbose, "verbose", false, "Toggle verbose logging (debug level)")
50
cmd.Flags().StringVar(&configPath, "config", localConfig, "Configuration file for running usage component")
51
52
return cmd
53
}
54
55
func parseConfig(path string) (server.Config, error) {
56
raw, err := os.ReadFile(path)
57
if err != nil {
58
return server.Config{}, fmt.Errorf("failed to read config from %s: %w", path, err)
59
}
60
61
var cfg server.Config
62
dec := json.NewDecoder(bytes.NewReader(raw))
63
dec.DisallowUnknownFields()
64
err = dec.Decode(&cfg)
65
if err != nil {
66
return server.Config{}, fmt.Errorf("failed to parse config from %s: %w", path, err)
67
}
68
69
return cfg, nil
70
}
71
72