github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+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/cmd/helm/helmpath"
    28  	"k8s.io/helm/pkg/repo"
    29  )
    30  
    31  const serveDesc = `
    32  This command starts a local chart repository server that serves charts from a local directory.
    33  
    34  The new server will provide HTTP access to a repository. By default, it will
    35  scan all of the charts in '$HELM_HOME/repository/local' and serve those over
    36  the a local IPv4 TCP port (default '127.0.0.1:8879').
    37  `
    38  
    39  type serveCmd struct {
    40  	out      io.Writer
    41  	address  string
    42  	repoPath string
    43  }
    44  
    45  func newServeCmd(out io.Writer) *cobra.Command {
    46  	srv := &serveCmd{out: out}
    47  	cmd := &cobra.Command{
    48  		Use:   "serve",
    49  		Short: "start a local http web server",
    50  		Long:  serveDesc,
    51  		RunE: func(cmd *cobra.Command, args []string) error {
    52  			return srv.run()
    53  		},
    54  	}
    55  
    56  	f := cmd.Flags()
    57  	f.StringVar(&srv.repoPath, "repo-path", helmpath.Home(homePath()).LocalRepository(), "local directory path from which to serve charts")
    58  	f.StringVar(&srv.address, "address", "127.0.0.1:8879", "address to listen on")
    59  
    60  	return cmd
    61  }
    62  
    63  func (s *serveCmd) run() error {
    64  	repoPath, err := filepath.Abs(s.repoPath)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	if _, err := os.Stat(repoPath); os.IsNotExist(err) {
    69  		return err
    70  	}
    71  
    72  	fmt.Fprintln(s.out, "Regenerating index. This may take a moment.")
    73  	if err := index(repoPath, "http://"+s.address, ""); err != nil {
    74  		return err
    75  	}
    76  
    77  	fmt.Fprintf(s.out, "Now serving you on %s\n", s.address)
    78  	return repo.StartLocalRepo(repoPath, s.address)
    79  }