github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+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/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 local IPv4 TCP port (default '127.0.0.1:8879'). 37 ` 38 39 type serveCmd struct { 40 out io.Writer 41 url string 42 address string 43 repoPath string 44 } 45 46 func newServeCmd(out io.Writer) *cobra.Command { 47 srv := &serveCmd{out: out} 48 cmd := &cobra.Command{ 49 Use: "serve", 50 Short: "start a local http web server", 51 Long: serveDesc, 52 RunE: func(cmd *cobra.Command, args []string) error { 53 return srv.run() 54 }, 55 } 56 57 f := cmd.Flags() 58 f.StringVar(&srv.repoPath, "repo-path", helmpath.Home(homePath()).LocalRepository(), "local directory path from which to serve charts") 59 f.StringVar(&srv.address, "address", "127.0.0.1:8879", "address to listen on") 60 f.StringVar(&srv.url, "url", "", "external URL of chart repository") 61 62 return cmd 63 } 64 65 func (s *serveCmd) run() error { 66 repoPath, err := filepath.Abs(s.repoPath) 67 if err != nil { 68 return err 69 } 70 if _, err := os.Stat(repoPath); os.IsNotExist(err) { 71 return err 72 } 73 74 fmt.Fprintln(s.out, "Regenerating index. This may take a moment.") 75 if len(s.url) > 0 { 76 err = index(repoPath, s.url, "") 77 } else { 78 err = index(repoPath, "http://"+s.address, "") 79 } 80 if err != nil { 81 return err 82 } 83 84 fmt.Fprintf(s.out, "Now serving you on %s\n", s.address) 85 return repo.StartLocalRepo(repoPath, s.address) 86 }