github.com/GoogleCloudPlatform/terraformer@v0.8.18/cmd/provider_cmd_github.go (about)

     1  // Copyright 2018 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package cmd
    15  
    16  import (
    17  	"log"
    18  	"strings"
    19  
    20  	github_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/github"
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  func newCmdGithubImporter(options ImportOptions) *cobra.Command {
    26  	token := ""
    27  	baseURL := ""
    28  	owner := []string{}
    29  	cmd := &cobra.Command{
    30  		Use:   "github",
    31  		Short: "Import current state to Terraform configuration from GitHub",
    32  		Long:  "Import current state to Terraform configuration from GitHub",
    33  		RunE: func(cmd *cobra.Command, args []string) error {
    34  			originalPathPattern := options.PathPattern
    35  			for _, organization := range owner {
    36  				provider := newGitHubProvider()
    37  				options.PathPattern = originalPathPattern
    38  				options.PathPattern = strings.ReplaceAll(options.PathPattern, "{provider}", "{provider}/"+organization)
    39  				log.Println(provider.GetName() + " importing organization " + organization)
    40  				err := Import(provider, options, []string{organization, token, baseURL})
    41  				if err != nil {
    42  					return err
    43  				}
    44  			}
    45  			return nil
    46  		},
    47  	}
    48  	cmd.AddCommand(listCmd(newGitHubProvider()))
    49  	baseProviderFlags(cmd.PersistentFlags(), &options, "repository", "repository=id1:id2:id4")
    50  	cmd.PersistentFlags().StringVarP(&token, "token", "t", "", "YOUR_GITHUB_TOKEN or env param GITHUB_TOKEN")
    51  	cmd.PersistentFlags().StringSliceVarP(&owner, "owner", "", []string{}, "")
    52  	cmd.PersistentFlags().StringVarP(&baseURL, "base-url", "", "", "")
    53  	return cmd
    54  }
    55  
    56  func newGitHubProvider() terraformutils.ProviderGenerator {
    57  	return &github_terraforming.GithubProvider{}
    58  }