github.com/actions-on-google/gactions@v3.2.0+incompatible/cmd/gactions/cli/notices/notices.go (about)

     1  // Copyright 2020 Google LLC
     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  //     https://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 notices provides an implementation of "gactions third-party-notices" command.
    16  package notices
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"github.com/actions-on-google/gactions/log"
    22  	"github.com/spf13/cobra"
    23  	"gopkg.in/yaml.v2"
    24  )
    25  
    26  type licenseObj struct {
    27  	Title   string `yaml:"title"`
    28  	Content string `yaml:"content"`
    29  }
    30  
    31  func parse(v []byte) ([]licenseObj, error) {
    32  	obj := []licenseObj{}
    33  	if err := yaml.Unmarshal(v, &obj); err != nil {
    34  		return nil, err
    35  	}
    36  	return obj, nil
    37  }
    38  
    39  // AddCommand adds the push sub-command to the passed in root command.
    40  func AddCommand(root *cobra.Command) {
    41  	notices := &cobra.Command{
    42  		Use:   "third-party-notices",
    43  		Short: "Prints license files of third-party software used.",
    44  		Long:  "Prints license files of third-party software used in CLI source code.",
    45  		Run: func(cmd *cobra.Command, args []string) {
    46  			// licenseFiles is a map where a title is the name of the library and content is its license.
    47  			for _, v := range licenseFiles {
    48  				licenses, err := parse(v)
    49  				if err != nil {
    50  					fmt.Println(err)
    51  					return
    52  				}
    53  				for _, v := range licenses {
    54  					log.Outf("Software: %s\n", string(v.Title))
    55  					log.Outf("License:\n%s\n", string(v.Content))
    56  				}
    57  			}
    58  		},
    59  		Args: cobra.NoArgs,
    60  	}
    61  	root.AddCommand(notices)
    62  }