github.com/charypar/monobuild@v0.0.0-20211122220434-fd884ed50212/cmd/print.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  
     8  	"github.com/charypar/monobuild/cli"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var printCmd = &cobra.Command{
    13  	Use:   "print",
    14  	Short: "Print the full build schedule or dependency graph",
    15  	Long: `Print the full build schedule or dependency graph based on the manifest files.
    16  The format of each line is:
    17  
    18  <component>: <dependency>, <dependency>, <dependency>, ...
    19  
    20  Diff can output either the build schedule (using only strong dependencies) or 
    21  the original dependeny graph (using all dependencies).`,
    22  	Run: printFn,
    23  }
    24  
    25  func init() {
    26  	rootCmd.AddCommand(printCmd)
    27  
    28  	printCmd.Flags().BoolVar(&commonOpts.printDependencies, "dependencies", false, "Ouput the dependencies, not the build schedule")
    29  	printCmd.Flags().BoolVar(&commonOpts.dotFormat, "dot", false, "Print in DOT format for GraphViz")
    30  	printCmd.Flags().BoolVar(&commonOpts.printFull, "full", false, "Print the full dependency graph including strengths")
    31  
    32  }
    33  
    34  func printFn(cmd *cobra.Command, args []string) {
    35  	// first we tediously process the CLI flags
    36  
    37  	var format cli.OutputFormat
    38  	if commonOpts.dotFormat {
    39  		format = cli.Dot
    40  	} else {
    41  		format = cli.Text
    42  	}
    43  
    44  	scope := cli.Scope{Scope: commonOpts.scope, TopLevel: commonOpts.topLevel}
    45  
    46  	var outType cli.OutputType
    47  	if commonOpts.printFull {
    48  		outType = cli.Full
    49  	} else if commonOpts.printDependencies {
    50  		outType = cli.Dependencies
    51  	} else {
    52  		outType = cli.Schedule
    53  	}
    54  
    55  	outputOpts := cli.OutputOptions{Format: format, Type: outType}
    56  
    57  	repoManifest := ""
    58  	if len(commonOpts.repoManifestFile) > 0 {
    59  		bytes, err := ioutil.ReadFile(commonOpts.repoManifestFile)
    60  		if err != nil {
    61  			fmt.Printf("Failed reading: %s", err)
    62  			log.Fatal(err)
    63  		}
    64  
    65  		repoManifest = string(bytes)
    66  	}
    67  
    68  	// then we run the CLI
    69  	dependencies, schedule, impacted, err := cli.Print(commonOpts.dependencyFilesGlob, scope, repoManifest)
    70  	if err != nil {
    71  		log.Fatal(err)
    72  	}
    73  
    74  	fmt.Print(cli.Format(dependencies, schedule, impacted, outputOpts))
    75  }