Path: blob/main/components/public-api-server/cmd/root.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"bytes"8"context"9"encoding/json"10"os"11"path"1213"github.com/gitpod-io/gitpod/components/public-api/go/config"1415"github.com/gitpod-io/gitpod/common-go/log"16"github.com/spf13/cobra"17)1819var (20// ServiceName is the name we use for tracing/logging21ServiceName = "public-api-server"22// Version of this service - set during build23Version = ""24)2526var rootOpts struct {27CfgFile string28JsonLog bool29Verbose bool30}3132// rootCmd represents the base command when called without any subcommands33var rootCmd = &cobra.Command{34Use: ServiceName,35Short: "Serves public API services",36PersistentPreRun: func(cmd *cobra.Command, args []string) {37log.Init(ServiceName, Version, rootOpts.JsonLog, rootOpts.Verbose)38},39}4041func Execute() {42if err := rootCmd.ExecuteContext(context.Background()); err != nil {43log.WithError(err).Error("Failed to execute command.")44os.Exit(1)45}46}4748func init() {49localConfig := path.Join(os.ExpandEnv("GOMOD"), "..", "config.json")50rootCmd.PersistentFlags().StringVar(&rootOpts.CfgFile, "config", localConfig, "config file")51rootCmd.PersistentFlags().BoolVar(&rootOpts.JsonLog, "json-log", true, "produce JSON log output on verbose level")52rootCmd.PersistentFlags().BoolVar(&rootOpts.Verbose, "verbose", false, "Enable verbose JSON logging")53}5455func getConfig() *config.Configuration {56ctnt, err := os.ReadFile(rootOpts.CfgFile)57if err != nil {58log.WithError(err).Fatal("Cannot read configuration. Maybe missing --config?")59}6061var cfg config.Configuration62dec := json.NewDecoder(bytes.NewReader(ctnt))63dec.DisallowUnknownFields()64err = dec.Decode(&cfg)65if err != nil {66log.WithError(err).Fatal("Cannot decode configuration. Maybe missing --config?")67}6869return &cfg70}717273