github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/config/cmd/builder.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package cmd
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"sort"
    24  	"strings"
    25  
    26  	defaults "github.com/mcuadros/go-defaults"
    27  	toml "github.com/pelletier/go-toml"
    28  	"github.com/spf13/cobra"
    29  	"go.uber.org/zap"
    30  
    31  	"github.com/zntrio/harp/v2/pkg/sdk/flags"
    32  	"github.com/zntrio/harp/v2/pkg/sdk/log"
    33  )
    34  
    35  var configNewAsEnvFlag bool
    36  
    37  // NewConfigCommand initialize a cobra config command tree.
    38  func NewConfigCommand(conf interface{}, envPrefix string) *cobra.Command {
    39  	// Uppercase the prefix
    40  	upPrefix := strings.ToUpper(envPrefix)
    41  
    42  	// config
    43  	configCmd := &cobra.Command{
    44  		Use:   "config",
    45  		Short: "Manage Service Configuration",
    46  	}
    47  
    48  	// config new
    49  	configNewCmd := &cobra.Command{
    50  		Use:   "new",
    51  		Short: "Initialize a default configuration",
    52  		Run: func(cmd *cobra.Command, args []string) {
    53  			defaults.SetDefaults(conf)
    54  
    55  			if !configNewAsEnvFlag {
    56  				btes, err := toml.Marshal(conf)
    57  				if err != nil {
    58  					log.For(cmd.Context()).Fatal("Error during configuration export", zap.Error(err))
    59  				}
    60  				fmt.Fprintln(os.Stdout, string(btes))
    61  			} else {
    62  				m := flags.AsEnvVariables(conf, upPrefix, true)
    63  				keys := []string{}
    64  
    65  				for k := range m {
    66  					keys = append(keys, k)
    67  				}
    68  
    69  				sort.Strings(keys)
    70  				for _, k := range keys {
    71  					fmt.Fprintf(os.Stdout, "export %s=\"%s\"\n", k, m[k])
    72  				}
    73  			}
    74  		},
    75  	}
    76  
    77  	// flags
    78  	configNewCmd.Flags().BoolVar(&configNewAsEnvFlag, "env", false, "Print configuration as environment variable")
    79  	configCmd.AddCommand(configNewCmd)
    80  
    81  	// Return base command
    82  	return configCmd
    83  }