Path: blob/main/components/image-builder-mk3/cmd/root.go
2497 views
// Copyright (c) 2021 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"encoding/json"8"fmt"9"github.com/gitpod-io/gitpod/image-builder/api/config"10"os"1112"github.com/mattn/go-isatty"13"github.com/spf13/cobra"14"golang.org/x/xerrors"1516"github.com/gitpod-io/gitpod/common-go/log"17"github.com/gitpod-io/gitpod/common-go/tracing"18)1920var (21// ServiceName is the name we use for tracing/logging22ServiceName = "image-builder-mk3"23// Version of this service - set during build24Version = ""25)2627var jsonLog bool28var verbose bool29var configFile string30var rootCmd = &cobra.Command{31Use: "image-builder-mk3",32Short: "Workspace image-builder service",33PersistentPreRun: func(cmd *cobra.Command, args []string) {34log.Init(ServiceName, Version, jsonLog && !isatty.IsTerminal(os.Stdout.Fd()), verbose)35},36}3738// Execute runs this main command39func Execute() {40closer := tracing.Init(ServiceName)41if closer != nil {42defer closer.Close()43}4445if err := rootCmd.Execute(); err != nil {46fmt.Println(err)47os.Exit(1)48}49}5051func getConfig() *config.ServiceConfig {52ctnt, err := os.ReadFile(configFile)53if err != nil {54log.WithError(xerrors.Errorf("cannot read config: %w", err)).Error("cannot read configuration. Maybe missing --config?")55os.Exit(1)56}5758var cfg config.ServiceConfig59err = json.Unmarshal(ctnt, &cfg)60if err != nil {61log.WithError(err).Error("cannot read configuration. Maybe missing --config?")62os.Exit(1)63}6465return &cfg66}6768func init() {69rootCmd.PersistentFlags().BoolVarP(&jsonLog, "json-log", "j", true, "produce JSON log output on verbose level")70rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose JSON logging")71rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file")72}737475