func (c *BuildCommand) Run(args []string) int { var pipelineName string var runId int flags := c.Meta.FlagSet("client", FlagSetClient) flags.Usage = func() { c.Ui.Output(c.Help()) } flags.StringVar(&pipelineName, "pipeline", "", "Name of target pipeline") flags.IntVar(&runId, "run_id", 0, "Run ID") if err := flags.Parse(args); err != nil { log.Errorf("Failes to parse flags: %v", err) return 1 } var logOutput io.Writer if c.Meta.logOutput != "" { fi, err := os.OpenFile(c.Meta.logOutput, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Errorf("Failed to open log output file '%s'. Error: %s\n", c.Meta.logOutput, err) return -1 } defer fi.Close() logOutput = fi } else { logOutput = os.Stdout } util.ConfigureLogging(c.Meta.logLevel, c.Meta.logFormat, logOutput) if pipelineName == "" { log.Errorf("Must provide a valid pipeline name") return 1 } if runId == 0 { log.Errorf("Must provide a valid run id") return 1 } return build.BuildPipeline(pipelineName, runId) }
func (c *CreatePipelineCommand) Run(args []string) int { flags := c.Meta.FlagSet("create-pipeline", FlagSetClient) flags.Usage = func() { c.Ui.Output(c.Help()) } if err := flags.Parse(args); err != nil { log.Errorf("Failed to parse cli arguments. Error: %s\n", err) return 1 } var logOutput io.Writer if c.Meta.logOutput != "" { fi, err := os.OpenFile(c.Meta.logOutput, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Errorf("Failed to open log output file '%s'. Error: %s\n", c.Meta.logOutput, err) return -1 } defer fi.Close() logOutput = fi } else { logOutput = os.Stdout } util.ConfigureLogging(c.Meta.logLevel, c.Meta.logFormat, logOutput) file := "gypsy.yml" args = flags.Args() if len(args) > 0 { file = args[0] } client, err := c.Meta.Client() if err != nil { log.Errorf("Failed to create api client. Error:%s\n", err) return -1 } content, err := ioutil.ReadFile(file) if err != nil { log.Errorf("Failed to read file %s . Error: %s\n", file, err) return -1 } var pipeline structs.Pipeline if err := yaml.Unmarshal(content, &pipeline); err != nil { log.Errorf("Failed to unmarshal pipeline : %v", err) return -1 } if err := client.CreatePipeline(&pipeline); err != nil { log.Errorf("Failed to create pipeline. Error: %s\n", err) return -1 } c.Ui.Output("Sucessfully created pipeline " + pipeline.Name) return 0 }
func (c *DockerfileCommand) Run(args []string) int { var file string var name string flags := c.Meta.FlagSet("dockerfile", FlagSetClient) flags.StringVar(&file, "file", "Dockerfile", "Path to dockerfile like specification") flags.StringVar(&name, "name", "", "Name of the container (default will be autogenerated uuid)") flags.Usage = func() { c.Ui.Output(c.Help()) } if err := flags.Parse(args); err != nil { log.Errorf("Failed to parse cli arguments. Error: %s\n", err) return 1 } var logOutput io.Writer if c.Meta.logOutput != "" { fi, err := os.OpenFile(c.Meta.logOutput, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Errorf("Failed to open log output file '%s'. Error: %s\n", c.Meta.logOutput, err) return -1 } defer fi.Close() logOutput = fi } else { logOutput = os.Stdout } util.ConfigureLogging(c.Meta.logLevel, c.Meta.logFormat, logOutput) if name == "" { id, err := util.UUID() if err != nil { log.Errorf("Failed to generate uuid. Error: %s\n", err) return -1 } name = id log.Infof("No name given. Using uuid %s\n", name) } spec := dockerfile.NewSpec(name, file) if err := spec.Parse(); err != nil { log.Errorf("Failed to parse dockerfile. Error: %s\n", err) return -1 } log.Debugf("Successfully parse dockerfile") if err := spec.Build(); err != nil { log.Errorf("Failed to build container from dockerfile. Error: %s\n", err) return -1 } log.Debugf("Successfully parse dockerfile") return 0 }
func (c *DeletePipelineCommand) Run(args []string) int { var file string flags := c.Meta.FlagSet("create-pipeline", FlagSetClient) flags.StringVar(&file, "file", "gypsy.yml", "Path to yaml file containing pipeline details") flags.Usage = func() { c.Ui.Output(c.Help()) } if err := flags.Parse(args); err != nil { log.Errorf("Failed to parse cli arguments. Error: %s\n", err) return 1 } var logOutput io.Writer if c.Meta.logOutput != "" { fi, err := os.OpenFile(c.Meta.logOutput, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Errorf("Failed to open log output file '%s'. Error: %s\n", c.Meta.logOutput, err) return -1 } defer fi.Close() logOutput = fi } else { logOutput = os.Stdout } util.ConfigureLogging(c.Meta.logLevel, c.Meta.logFormat, logOutput) args = flags.Args() if len(args) != 1 { c.Ui.Error(c.Help()) return -1 } client, err := c.Meta.Client() if err != nil { log.Errorf("Failed to create api client. Error:%s\n", err) return -1 } if err := client.DeletePipeline(args[0]); err != nil { log.Errorf("Failed to delete pipeline. Error:%s\n", err) return -1 } c.Ui.Output("Suscessfully deleted " + args[0]) return 0 }
func (c *ShowPipelineCommand) Run(args []string) int { flags := c.Meta.FlagSet("show-pipeline", FlagSetClient) flags.Usage = func() { c.Ui.Output(c.Help()) } if err := flags.Parse(args); err != nil { log.Errorf("Failed to parse cli arguments. Error: %s\n", err) return 1 } var logOutput io.Writer if c.Meta.logOutput != "" { fi, err := os.OpenFile(c.Meta.logOutput, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Errorf("Failed to open log output file '%s'. Error: %s\n", c.Meta.logOutput, err) return -1 } defer fi.Close() logOutput = fi } else { logOutput = os.Stdout } util.ConfigureLogging(c.Meta.logLevel, c.Meta.logFormat, logOutput) args = flags.Args() if len(args) != 1 { c.Ui.Error(c.Help()) return -1 } client, err := c.Meta.Client() if err != nil { log.Errorf("Failed to create api client. Error:%s\n", err) return -1 } pipeline, err := client.GetPipeline(args[0]) if err != nil { log.Errorf("Failed to obtain pipeline data. Error:%s\n", err) return -1 } c.Ui.Output(pipeline) return 0 }
func (c *ListPipelineCommand) Run(args []string) int { flags := c.Meta.FlagSet("list-pipeline", FlagSetClient) flags.Usage = func() { c.Ui.Output(c.Help()) } if err := flags.Parse(args); err != nil { log.Errorf("Failed to parse cli arguments. Error: %s\n", err) return 1 } var logOutput io.Writer if c.Meta.logOutput != "" { fi, err := os.OpenFile(c.Meta.logOutput, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Errorf("Failed to open log output file '%s'. Error: %s\n", c.Meta.logOutput, err) return -1 } defer fi.Close() logOutput = fi } else { logOutput = os.Stdout } util.ConfigureLogging(c.Meta.logLevel, c.Meta.logFormat, logOutput) client, err := c.Meta.Client() if err != nil { log.Errorf("Failed to create api client. Error:%s\n", err) return -1 } pipelines, err := client.ListPipelines() if err != nil { log.Errorf("Failed to list pipeline. Error: %s\n", err) return -1 } c.Ui.Output(fmt.Sprintf("%6s %-30s", "#", "pipeline")) for i, pipeline := range pipelines { c.Ui.Output(fmt.Sprintf("%6d %-30s", i+1, pipeline)) } return 0 }