github.com/aavshr/aws-sdk-go@v1.41.3/example/service/ecr/createRepository/createRepository.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  
    10  	"github.com/aavshr/aws-sdk-go/aws"
    11  	"github.com/aavshr/aws-sdk-go/aws/session"
    12  	"github.com/aavshr/aws-sdk-go/service/ecr"
    13  )
    14  
    15  const DEFAULT_AWS_REGION = "us-east-1"
    16  
    17  // This example creates a new ECR Repository
    18  //
    19  // Usage:
    20  // AWS_REGION=us-east-1 go run -tags example createECRRepository.go <repo_name>
    21  func main() {
    22  
    23  	config := &aws.Config{Region: aws.String(getAwsRegion())}
    24  	svc := ecr.New(session.New(), config)
    25  
    26  	repoName := getRepoNameArg()
    27  	if repoName == "" {
    28  		printUsageAndExit1()
    29  	}
    30  	input := &ecr.CreateRepositoryInput{
    31  		RepositoryName: aws.String(repoName),
    32  	}
    33  
    34  	output, err := svc.CreateRepository(input)
    35  	if err != nil {
    36  		fmt.Printf("\nError creating the repo %v in region %v\n%v\n", repoName, aws.StringValue(config.Region), err.Error())
    37  		os.Exit(1)
    38  	}
    39  
    40  	fmt.Printf("\nECR Repository \"%v\" created successfully!\n\nAWS Output:\n%v", repoName, output)
    41  }
    42  
    43  // Print correct usage and exit the program with code 1
    44  func printUsageAndExit1() {
    45  	fmt.Println("\nUsage: AWS_REGION=us-east-1 go run -tags example createECRRepository.go <repo_name>")
    46  	os.Exit(1)
    47  }
    48  
    49  // Try get the repo name from the first argument
    50  func getRepoNameArg() string {
    51  	if len(os.Args) < 2 {
    52  		return ""
    53  	}
    54  	firstArg := os.Args[1]
    55  	return firstArg
    56  }
    57  
    58  // Returns the aws region from env var or default region defined in DEFAULT_AWS_REGION constant
    59  func getAwsRegion() string {
    60  	awsRegion := os.Getenv("AWS_REGION")
    61  	if awsRegion != "" {
    62  		return awsRegion
    63  	}
    64  	return DEFAULT_AWS_REGION
    65  }