예제 #1
0
파일: plans.go 프로젝트: iwarsong/bearded
func plansShowAction(ctx *cli.Context, api *client.Client, timeout Timeout) {
	if len(ctx.Args()) == 0 {
		fmt.Printf("You should set plan id argument: plans show [id]\n")
		os.Exit(1)
	}
	plan, err := api.Plans.Get(timeout(), ctx.Args()[0])
	if err != nil {
		if client.IsNotFound(err) {
			fmt.Println("Plan not found")
			return
		}
		fmt.Print(err)
		return
	}
	fmt.Println("Plan found:")
	data, err := json.MarshalIndent(plan, "", "    ")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(data))
	return
}
예제 #2
0
파일: plans.go 프로젝트: iwarsong/bearded
func plansLoadAction(ctx *cli.Context, api *client.Client, timeout Timeout) {
	if len(ctx.Args()) == 0 {
		fmt.Printf("You should set filename argument: f.e plans load ./extra/data/plans.json\n")
		os.Exit(1)
	}
	filename := ctx.Args()[0]
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		if !os.IsExist(err) {
			fmt.Printf("File %s is not existed\n", filename)
			os.Exit(1)
		}
		panic(err)
	}
	plans := []*plan.Plan{}
	if err := json.Unmarshal(data, &plans); err != nil {
		panic(err)
	}
	update := ctx.Bool("update")
	if update {
		fmt.Println("Autoupdate is enabled")
	}
	fmt.Printf("Found %d plans\n", len(plans))
	for i, p := range plans {
		fmt.Printf("%d) %s\n", i, p)
		fmt.Printf("Creating..\n")

		_, err := api.Plans.Create(timeout(), p)
		if err != nil {
			if client.IsConflicted(err) {
				fmt.Println("Plan with this name is already existed")
				if update {
					fmt.Println("Updating..")
					// retreive existed plan
					planList, err := api.Plans.List(timeout(), &client.PlansListOpts{Name: p.Name})
					if err != nil {
						panic(err)
					}
					if planList.Count != 1 {
						err := fmt.Errorf("Expected 1 plan, but actual is %d", planList.Count)
						panic(err)
					}

					// update it
					p.Id = planList.Results[0].Id
					_, err = api.Plans.Update(timeout(), p)
					if err != nil {
						fmt.Printf("Plugin updating failed, because: %v", err)
						continue
					}
				} else {
					continue
				}
			} else {
				fmt.Printf("Plan wasn't created because: %v", err)
				continue
			}
		}
		fmt.Println("Successful")
		//		fmt.Printf("%s\n", created)
	}

}