Beispiel #1
0
func Execute() {
	var (
		req       sti.Request
		envString string
		buildReq  sti.BuildRequest
	)

	stiCmd := &cobra.Command{
		Use:   "sti",
		Short: "STI is a tool for building repeatable docker images",
		Long: `A command-line interface for the sti library
              Complete documentation is available at http://github.com/pmorie/go-sti`,
		Run: func(cmd *cobra.Command, args []string) {
			cmd.Usage()
		},
	}
	stiCmd.PersistentFlags().StringVarP(&(req.DockerSocket), "url", "U", "unix:///var/run/docker.sock", "Set the url of the docker socket to use")
	stiCmd.PersistentFlags().BoolVar(&(req.Verbose), "verbose", false, "Enable verbose output")

	versionCmd := &cobra.Command{
		Use:   "version",
		Short: "Display version",
		Long:  "Display version",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Printf("sti %s\n", version)
		},
	}

	stiCmd.AddCommand(versionCmd)

	buildCmd := &cobra.Command{
		Use:   "build SOURCE BUILD_IMAGE APP_IMAGE_TAG",
		Short: "Build an image",
		Long:  "Build an image",
		Run: func(cmd *cobra.Command, args []string) {
			// if we're not verbose, make sure the logger doesn't print out timestamps
			if !req.Verbose {
				log.SetFlags(0)
			}

			if len(args) == 0 {
				cmd.Usage()
				return
			}

			buildReq.Request = req
			buildReq.Source = args[0]
			buildReq.BaseImage = args[1]
			buildReq.Tag = args[2]
			buildReq.Writer = os.Stdout

			envs, _ := parseEnvs(envString)
			buildReq.Environment = envs

			if buildReq.WorkingDir == "tempdir" {
				var err error
				buildReq.WorkingDir, err = ioutil.TempDir("", "sti")
				if err != nil {
					fmt.Println(err.Error())
					return
				}
				defer os.Remove(buildReq.WorkingDir)
			}

			res, err := sti.Build(buildReq)
			if err != nil {
				fmt.Printf("An error occured: %s\n", err.Error())
				return
			}

			for _, message := range res.Messages {
				fmt.Println(message)
			}
		},
	}
	buildCmd.Flags().BoolVar(&(buildReq.Clean), "clean", false, "Perform a clean build")
	buildCmd.Flags().StringVar(&(req.WorkingDir), "dir", "tempdir", "Directory where generated Dockerfiles and other support scripts are created")
	buildCmd.Flags().StringVarP(&envString, "env", "e", "", "Specify an environment var NAME=VALUE,NAME2=VALUE2,...")
	buildCmd.Flags().StringVarP(&(buildReq.Ref), "ref", "r", "", "Specify a ref to check-out")
	buildCmd.Flags().StringVar(&(buildReq.CallbackUrl), "callbackUrl", "", "Specify a URL to invoke via HTTP POST upon build completion")
	buildCmd.Flags().StringVarP(&(buildReq.ScriptsUrl), "scripts", "s", "", "Specify a URL for the assemble and run scripts")

	stiCmd.AddCommand(buildCmd)

	usageCmd := &cobra.Command{
		Use:   "usage BUILD_IMAGE",
		Short: "Print usage for assemble script associated with an image",
		Long:  "Print usage for assemble script associated with an image",
		Run: func(cmd *cobra.Command, args []string) {
			// if we're not verbose, make sure the logger doesn't print out timestamps
			if !req.Verbose {
				log.SetFlags(0)
			}

			if len(args) == 0 {
				cmd.Usage()
				return
			}

			buildReq.Request = req
			buildReq.BaseImage = args[0]
			buildReq.Writer = os.Stdout

			envs, _ := parseEnvs(envString)
			buildReq.Environment = envs

			if buildReq.WorkingDir == "tempdir" {
				var err error
				buildReq.WorkingDir, err = ioutil.TempDir("", "sti")
				if err != nil {
					fmt.Println(err.Error())
					return
				}
				defer os.Remove(buildReq.WorkingDir)
			}

			res, err := sti.Usage(buildReq)
			if err != nil {
				fmt.Printf("An error occured: %s\n", err.Error())
				return
			}

			for _, message := range res.Messages {
				fmt.Println(message)
			}
		},
	}
	usageCmd.Flags().StringVar(&(req.WorkingDir), "dir", "tempdir", "Directory where generated Dockerfiles and other support scripts are created")
	usageCmd.Flags().StringVarP(&envString, "env", "e", "", "Specify an environment var NAME=VALUE,NAME2=VALUE2,...")
	usageCmd.Flags().StringVarP(&(buildReq.ScriptsUrl), "scripts", "s", "", "Specify a URL for the assemble and run scripts")

	stiCmd.AddCommand(usageCmd)

	stiCmd.Execute()
}
Beispiel #2
0
func Execute() {
	var (
		req       *sti.STIRequest
		envString string
	)

	req = &sti.STIRequest{}

	stiCmd := &cobra.Command{
		Use: "sti",
		Long: "Source-to-images (STI) is a tool for building repeatable docker images.\n\n" +
			"A command line interface that injects and assembles an application source into a docker image.\n" +
			"Complete documentation is available at http://github.com/openshift/geard/tree/master/sti",
		Run: func(c *cobra.Command, args []string) {
			c.Help()
		},
	}
	stiCmd.PersistentFlags().StringVarP(&(req.DockerSocket), "url", "U", "unix:///var/run/docker.sock", "Set the url of the docker socket to use")
	stiCmd.PersistentFlags().BoolVar(&(req.Verbose), "verbose", false, "Enable verbose output")
	stiCmd.PersistentFlags().BoolVar(&(req.PreserveWorkingDir), "savetempdir", false, "Save the temporary directory used by STI instead of deleting it")

	versionCmd := &cobra.Command{
		Use:   "version",
		Short: "Display version",
		Long:  "Display version",
		Run: func(c *cobra.Command, args []string) {
			fmt.Printf("sti %s\n", version)
		},
	}

	stiCmd.AddCommand(versionCmd)

	buildCmd := &cobra.Command{
		Use:   "build <source> <image> <tag>",
		Short: "Build a new image",
		Long:  "Build a new Docker image named <tag> from a source repository and base image.",
		Run: func(c *cobra.Command, args []string) {
			// if we're not verbose, make sure the logger doesn't print out timestamps
			if !req.Verbose {
				log.SetFlags(0)
			}

			if len(args) < 3 {
				fmt.Println("Valid arguments: <source> <image> <tag> ...")
				os.Exit(1)
			}

			req.Source = args[0]
			req.BaseImage = args[1]
			req.Tag = args[2]
			req.Writer = os.Stdout

			envs, _ := parseEnvs(envString)
			req.Environment = envs

			res, err := sti.Build(req)
			if err != nil {
				fmt.Printf("An error occured: %s\n", err.Error())
				os.Exit(1)
			}

			for _, message := range res.Messages {
				fmt.Println(message)
			}
		},
	}
	buildCmd.Flags().BoolVar(&(req.Clean), "clean", false, "Perform a clean build")
	buildCmd.Flags().BoolVar(&(req.RemovePreviousImage), "rm", false, "Remove the previous image during incremental builds")
	buildCmd.Flags().StringVarP(&envString, "env", "e", "", "Specify an environment var NAME=VALUE,NAME2=VALUE2,...")
	buildCmd.Flags().StringVarP(&(req.Ref), "ref", "r", "", "Specify a ref to check-out")
	buildCmd.Flags().StringVar(&(req.CallbackUrl), "callbackUrl", "", "Specify a URL to invoke via HTTP POST upon build completion")
	buildCmd.Flags().StringVarP(&(req.ScriptsUrl), "scripts", "s", "", "Specify a URL for the assemble and run scripts")

	stiCmd.AddCommand(buildCmd)

	usageCmd := &cobra.Command{
		Use:   "usage <image>",
		Short: "Print usage of the assemble script associated with the image",
		Long:  "Create and start a container from the image and invoke it's usage (run `assemble -h' script).",
		Run: func(c *cobra.Command, args []string) {
			// if we're not verbose, make sure the logger doesn't print out timestamps
			if !req.Verbose {
				log.SetFlags(0)
			}

			if len(args) == 0 {
				fmt.Println("Valid arguments: <image>")
				os.Exit(1)
			}

			req.BaseImage = args[0]
			req.Writer = os.Stdout

			envs, _ := parseEnvs(envString)
			req.Environment = envs

			err := sti.Usage(req)
			if err != nil {
				fmt.Printf("An error occured: %s\n", err.Error())
				os.Exit(1)
			}
		},
	}
	usageCmd.Flags().StringVarP(&envString, "env", "e", "", "Specify an environment var NAME=VALUE,NAME2=VALUE2,...")
	usageCmd.Flags().StringVarP(&(req.ScriptsUrl), "scripts", "s", "", "Specify a URL for the assemble and run scripts")

	stiCmd.AddCommand(usageCmd)

	stiCmd.Execute()
}