func main() { // Create API instance api := cc.NewAPI() // Create new user john, err := api.CreateUser("john", "*****@*****.**", "secret") if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println(john.Username, "was created.") // Activate User with the code provided by email john, err = api.ActivateUser("json", "activationcode") if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println(john.Username, "is active.") // Basic authentication to API using email and password err = api.CreateToken(john.Email, "secret") if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println("Authenticated as", john.Username) // Create Application myapp, err := api.CreateApplication("myapp", "ruby", "git", "") if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println(myapp.Name, "was created.") // Add user to the application _, err = api.CreateAppUser("myapp", "*****@*****.**", "") if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println(john.Username, "was added to", myapp.Name) }
func run(workspace *drone.Workspace, build *drone.Build, vargs *Params) error { repo.GlobalName(build).Run() repo.GlobalUser(build).Run() api := cclib.NewAPI() api.SetUrl("https://api.cloudcontrolled.com") if err := api.CreateToken(vargs.Email, vargs.Password); err != nil { return errors.New("Failed to authenticate with email/password") } if _, err := api.ReadApplication(vargs.Application); err != nil { return errors.New("Failed to find the requested application") } if workspace.Keys != nil && len(workspace.Keys.Public) > 0 { publicKey, err := api.CreateUserKey( vargs.Email, workspace.Keys.Public) if err != nil { return errors.New("Failed to create a deployment key") } defer func() { api.DeleteUserKey( vargs.Email, publicKey.Id) }() } if err := repo.WriteKey(workspace); err != nil { return err } defer func() { execute( repo.RemoteRemove( "deploy"), workspace) }() cmd := repo.RemoteAdd( "deploy", remote(vargs)) if err := execute(cmd, workspace); err != nil { return err } if vargs.Commit { if err := execute(repo.ForceAdd(), workspace); err != nil { return err } if err := execute(repo.ForceCommit(), workspace); err != nil { return err } } cmd = repo.RemotePush( "deploy", vargs.Deployment, vargs.Force) if err := execute(cmd, workspace); err != nil { return err } if _, err := api.UpdateDeployment(vargs.Application, vargs.Deployment, "", "", "", 0, 0); err != nil { return errors.New("Failed to trigger the final deployment") } return nil }