Add personal and public options to bootstrap

pull/31/head
stefanprodan 5 years ago
parent 59ae2f327c
commit d4cbc45e16

@ -39,8 +39,11 @@ the bootstrap command will perform an upgrade if needed.`,
Example: ` # Create a GitHub personal access token and export it as an env var Example: ` # Create a GitHub personal access token and export it as an env var
export GITHUB_TOKEN=<my-token> export GITHUB_TOKEN=<my-token>
# Run bootstrap # Run bootstrap for a private repo owned by a GitHub organization
tk bootstrap github --owner=<org or user> --repository=<repo name> tk bootstrap github --owner=<organization> --repository=<repo name>
# Run bootstrap for a public repository on a personal account
tk bootstrap github --owner=<user> --repository=<repo name> --private=false --personal=true
`, `,
RunE: bootstrapGitHubCmdRun, RunE: bootstrapGitHubCmdRun,
} }
@ -49,6 +52,8 @@ var (
ghOwner string ghOwner string
ghRepository string ghRepository string
ghInterval time.Duration ghInterval time.Duration
ghPersonal bool
ghPrivate bool
) )
const ghTokenName = "GITHUB_TOKEN" const ghTokenName = "GITHUB_TOKEN"
@ -56,17 +61,19 @@ const ghTokenName = "GITHUB_TOKEN"
func init() { func init() {
bootstrapGitHubCmd.Flags().StringVar(&ghOwner, "owner", "", "GitHub user or organization name") bootstrapGitHubCmd.Flags().StringVar(&ghOwner, "owner", "", "GitHub user or organization name")
bootstrapGitHubCmd.Flags().StringVar(&ghRepository, "repository", "", "GitHub repository name") bootstrapGitHubCmd.Flags().StringVar(&ghRepository, "repository", "", "GitHub repository name")
bootstrapGitHubCmd.Flags().BoolVar(&ghPersonal, "personal", false, "personal repository")
bootstrapGitHubCmd.Flags().BoolVar(&ghPrivate, "private", true, "private repository")
bootstrapGitHubCmd.Flags().DurationVar(&ghInterval, "interval", time.Minute, "sync interval") bootstrapGitHubCmd.Flags().DurationVar(&ghInterval, "interval", time.Minute, "sync interval")
bootstrapCmd.AddCommand(bootstrapGitHubCmd) bootstrapCmd.AddCommand(bootstrapGitHubCmd)
} }
func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error { func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
ghToken := os.Getenv(ghTokenName) ghToken := os.Getenv(ghTokenName)
ghURL := fmt.Sprintf("https://github.com/%s/%s", ghOwner, ghRepository)
if ghToken == "" { if ghToken == "" {
return fmt.Errorf("%s environment variable not found", ghTokenName) return fmt.Errorf("%s environment variable not found", ghTokenName)
} }
ghURL := fmt.Sprintf("https://github.com/%s/%s", ghOwner, ghRepository)
if ghOwner == "" || ghRepository == "" { if ghOwner == "" || ghRepository == "" {
return fmt.Errorf("owner and repository are required") return fmt.Errorf("owner and repository are required")
} }
@ -87,7 +94,7 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
// create GitHub repository if doesn't exists // create GitHub repository if doesn't exists
logAction("connecting to GitHub") logAction("connecting to GitHub")
if err := initGitHubRepository(ctx, ghOwner, ghRepository, ghToken); err != nil { if err := initGitHubRepository(ctx, ghOwner, ghRepository, ghToken, ghPrivate, ghPersonal); err != nil {
return err return err
} }
@ -182,17 +189,21 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func initGitHubRepository(ctx context.Context, owner, name, token string) error { func initGitHubRepository(ctx context.Context, owner, name, token string, isPrivate, isPersonal bool) error {
isPrivate := true autoInit := true
isAutoInit := true
auth := github.BasicAuthTransport{ auth := github.BasicAuthTransport{
Username: "git", Username: "git",
Password: token, Password: token,
} }
org := ""
if !isPersonal {
org = owner
}
client := github.NewClient(auth.Client()) client := github.NewClient(auth.Client())
_, _, err := client.Repositories.Create(ctx, owner, &github.Repository{ _, _, err := client.Repositories.Create(ctx, org, &github.Repository{
AutoInit: &isAutoInit, AutoInit: &autoInit,
Name: &name, Name: &name,
Private: &isPrivate, Private: &isPrivate,
}) })

Loading…
Cancel
Save