github.com/zsuzhengdu/helm@v3.0.0-beta.3+incompatible/cmd/helm/env.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"sort"
    23  
    24  	"helm.sh/helm/pkg/cli"
    25  
    26  	"github.com/spf13/cobra"
    27  
    28  	"helm.sh/helm/cmd/helm/require"
    29  )
    30  
    31  var (
    32  	envHelp = `
    33  Env prints out all the environment information in use by Helm.
    34  `
    35  )
    36  
    37  func newEnvCmd(out io.Writer) *cobra.Command {
    38  	o := &envOptions{}
    39  	o.settings = cli.New()
    40  
    41  	cmd := &cobra.Command{
    42  		Use:   "env",
    43  		Short: "Helm client environment information",
    44  		Long:  envHelp,
    45  		Args:  require.NoArgs,
    46  		RunE: func(cmd *cobra.Command, args []string) error {
    47  			return o.run(out)
    48  		},
    49  	}
    50  
    51  	return cmd
    52  }
    53  
    54  type envOptions struct {
    55  	settings *cli.EnvSettings
    56  }
    57  
    58  func (o *envOptions) run(out io.Writer) error {
    59  
    60  	// Sorting keys to display in alphabetical order
    61  	var keys []string
    62  	for k := range o.settings.EnvironmentVariables {
    63  		keys = append(keys, k)
    64  	}
    65  	sort.Strings(keys)
    66  
    67  	for _, k := range keys {
    68  		fmt.Printf("%s=\"%s\" \n", k, o.settings.EnvironmentVariables[k])
    69  	}
    70  	return nil
    71  }