github.com/valdemarpavesi/helm@v2.9.1+incompatible/cmd/helm/serve.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  	"os"
    23  	"path/filepath"
    24  
    25  	"github.com/spf13/cobra"
    26  
    27  	"k8s.io/helm/pkg/repo"
    28  )
    29  
    30  const serveDesc = `
    31  This command starts a local chart repository server that serves charts from a local directory.
    32  
    33  The new server will provide HTTP access to a repository. By default, it will
    34  scan all of the charts in '$HELM_HOME/repository/local' and serve those over
    35  the local IPv4 TCP port (default '127.0.0.1:8879').
    36  
    37  This command is intended to be used for educational and testing purposes only.
    38  It is best to rely on a dedicated web server or a cloud-hosted solution like
    39  Google Cloud Storage for production use.
    40  
    41  See https://github.com/kubernetes/helm/blob/master/docs/chart_repository.md#hosting-chart-repositories
    42  for more information on hosting chart repositories in a production setting.
    43  `
    44  
    45  type serveCmd struct {
    46  	out      io.Writer
    47  	url      string
    48  	address  string
    49  	repoPath string
    50  }
    51  
    52  func newServeCmd(out io.Writer) *cobra.Command {
    53  	srv := &serveCmd{out: out}
    54  	cmd := &cobra.Command{
    55  		Use:   "serve",
    56  		Short: "start a local http web server",
    57  		Long:  serveDesc,
    58  		PreRunE: func(cmd *cobra.Command, args []string) error {
    59  			return srv.complete()
    60  		},
    61  		RunE: func(cmd *cobra.Command, args []string) error {
    62  			return srv.run()
    63  		},
    64  	}
    65  
    66  	f := cmd.Flags()
    67  	f.StringVar(&srv.repoPath, "repo-path", "", "local directory path from which to serve charts")
    68  	f.StringVar(&srv.address, "address", "127.0.0.1:8879", "address to listen on")
    69  	f.StringVar(&srv.url, "url", "", "external URL of chart repository")
    70  
    71  	return cmd
    72  }
    73  
    74  func (s *serveCmd) complete() error {
    75  	if s.repoPath == "" {
    76  		s.repoPath = settings.Home.LocalRepository()
    77  	}
    78  	return nil
    79  }
    80  
    81  func (s *serveCmd) run() error {
    82  	repoPath, err := filepath.Abs(s.repoPath)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	if _, err := os.Stat(repoPath); os.IsNotExist(err) {
    87  		return err
    88  	}
    89  
    90  	fmt.Fprintln(s.out, "Regenerating index. This may take a moment.")
    91  	if len(s.url) > 0 {
    92  		err = index(repoPath, s.url, "")
    93  	} else {
    94  		err = index(repoPath, "http://"+s.address, "")
    95  	}
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	fmt.Fprintf(s.out, "Now serving you on %s\n", s.address)
   101  	return repo.StartLocalRepo(repoPath, s.address)
   102  }