...

Source file src/github.com/google/go-containerregistry/cmd/gcrane/main.go

Documentation: github.com/google/go-containerregistry/cmd/gcrane

     1  // Copyright 2018 Google LLC All Rights Reserved.
     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  
    15  package main
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"os/signal"
    21  
    22  	"github.com/google/go-containerregistry/cmd/crane/cmd"
    23  	gcmd "github.com/google/go-containerregistry/cmd/gcrane/cmd"
    24  	"github.com/google/go-containerregistry/pkg/crane"
    25  	"github.com/google/go-containerregistry/pkg/gcrane"
    26  	"github.com/google/go-containerregistry/pkg/logs"
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  func init() {
    31  	logs.Warn.SetOutput(os.Stderr)
    32  	logs.Progress.SetOutput(os.Stderr)
    33  }
    34  
    35  const (
    36  	use   = "gcrane"
    37  	short = "gcrane is a tool for managing container images on gcr.io and pkg.dev"
    38  )
    39  
    40  func main() {
    41  	options := []crane.Option{crane.WithAuthFromKeychain(gcrane.Keychain)}
    42  	// Same as crane, but override usage and keychain.
    43  	root := cmd.New(use, short, options)
    44  
    45  	// Add or override commands.
    46  	gcraneCmds := []*cobra.Command{gcmd.NewCmdList(), gcmd.NewCmdGc(), gcmd.NewCmdCopy(), cmd.NewCmdAuth(options, "gcrane", "auth")}
    47  
    48  	// Maintain a map of google-specific commands that we "override".
    49  	used := make(map[string]bool)
    50  	for _, cmd := range gcraneCmds {
    51  		used[cmd.Use] = true
    52  	}
    53  
    54  	// Remove those from crane's set of commands.
    55  	for _, cmd := range root.Commands() {
    56  		if _, ok := used[cmd.Use]; ok {
    57  			root.RemoveCommand(cmd)
    58  		}
    59  	}
    60  
    61  	// Add our own.
    62  	for _, cmd := range gcraneCmds {
    63  		root.AddCommand(cmd)
    64  	}
    65  
    66  	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
    67  	defer cancel()
    68  	if err := root.ExecuteContext(ctx); err != nil {
    69  		cancel()
    70  		os.Exit(1)
    71  	}
    72  }
    73  

View as plain text