github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/robots/coverage/cmd/downloader/downloader.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package downloader
    18  
    19  import (
    20  	"cloud.google.com/go/storage"
    21  	"context"
    22  	"fmt"
    23  	"io"
    24  	"k8s.io/test-infra/robots/coverage/downloader"
    25  	"os"
    26  
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  type flags struct {
    31  	outputFile       string
    32  	artifactsDirName string
    33  	profileName      string
    34  }
    35  
    36  // MakeCommand returns a `download` command.
    37  func MakeCommand() *cobra.Command {
    38  	flags := &flags{}
    39  	cmd := &cobra.Command{
    40  		Use:   "download [bucket] [prowjob]",
    41  		Short: "Finds and downloads the coverage profile file from the latest healthy build",
    42  		Long: `Finds and downloads the coverage profile file from the latest healthy build 
    43  stored in given gcs directory.`,
    44  		Run: func(cmd *cobra.Command, args []string) {
    45  			run(flags, cmd, args)
    46  		},
    47  	}
    48  	cmd.Flags().StringVarP(&flags.outputFile, "output", "o", "-", "output file")
    49  	cmd.Flags().StringVarP(&flags.artifactsDirName, "artifactsDir", "a", "artifacts", "artifact directory name in GCS")
    50  	cmd.Flags().StringVarP(&flags.profileName, "profile", "p", "coverage-profile", "code coverage profile file name in GCS")
    51  	return cmd
    52  }
    53  
    54  func run(flags *flags, cmd *cobra.Command, args []string) {
    55  	if len(args) != 2 {
    56  		fmt.Fprintln(os.Stderr, "Expected exactly two arguments: bucket & prowjob")
    57  		cmd.Usage()
    58  		os.Exit(2)
    59  	}
    60  
    61  	bucket := args[0]
    62  	prowjob := args[1]
    63  
    64  	var file io.WriteCloser
    65  	if flags.outputFile == "-" {
    66  		file = os.Stdout
    67  	} else {
    68  		file, err := os.Create(flags.outputFile)
    69  		if err != nil {
    70  			fmt.Fprintf(os.Stderr, "Failed to create output file: %v.", err)
    71  			os.Exit(1)
    72  		}
    73  		defer file.Close()
    74  	}
    75  	ctx := context.Background()
    76  	client, err := storage.NewClient(ctx)
    77  	if err != nil {
    78  		fmt.Fprintf(os.Stderr, "Failed to create new storage client: %v.\n", err)
    79  		os.Exit(1)
    80  	}
    81  
    82  	content, err := downloader.FindBaseProfile(ctx, client, bucket, prowjob, flags.artifactsDirName, flags.profileName)
    83  	if err != nil {
    84  		fmt.Fprintf(os.Stderr, "Failed to find base profile file: %v.\n", err)
    85  		os.Exit(1)
    86  	}
    87  	_, err = file.Write(content)
    88  	if err != nil {
    89  		fmt.Fprintf(os.Stderr, "Failed to write profile: %v.\n", err)
    90  		os.Exit(1)
    91  	}
    92  }