trpc.group/trpc-go/trpc-cmdline@v1.0.9/cmd/setup/setup.go (about)

     1  // Tencent is pleased to support the open source community by making tRPC available.
     2  //
     3  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     4  // All rights reserved.
     5  //
     6  // If you have downloaded a copy of the tRPC source code from Tencent,
     7  // please note that tRPC source code is licensed under the  Apache 2.0 License,
     8  // A copy of the Apache 2.0 License is included in this file.
     9  
    10  // Package setup provides setup command.
    11  package setup
    12  
    13  import (
    14  	"fmt"
    15  
    16  	"github.com/spf13/cobra"
    17  
    18  	"trpc.group/trpc-go/trpc-cmdline/config"
    19  	"trpc.group/trpc-go/trpc-cmdline/util/log"
    20  )
    21  
    22  // CMD returns setup command.
    23  func CMD() *cobra.Command {
    24  	setup := func(languages []string) error {
    25  		// Load dependencies according to languages specified.
    26  		deps, err := config.LoadDependencies(languages...)
    27  		if err != nil {
    28  			return fmt.Errorf("load dependencies failed: %w", err)
    29  		}
    30  		// Setup dependencies.
    31  		return config.SetupDependencies(deps)
    32  	}
    33  	setupCmd := &cobra.Command{
    34  		Use:           "setup",
    35  		Short:         "Initialize setup && Install dependency tools",
    36  		Long:          "Initialize setup && Install dependency tools.",
    37  		SilenceErrors: true,
    38  		RunE: func(cmd *cobra.Command, _ []string) error {
    39  			log.SetPrefix("[setup]")
    40  			log.SetVerbose(true)
    41  			log.Info("Initializing setup && Installing dependency tools")
    42  			lang, err := cmd.Flags().GetStringArray("lang")
    43  			if err != nil {
    44  				return fmt.Errorf("get lang flag err: %w", err)
    45  			}
    46  			force, err := cmd.Flags().GetBool("force")
    47  			if err != nil {
    48  				return fmt.Errorf("get force flag err: %w", err)
    49  			}
    50  			if _, err := config.Init(config.WithForce(force)); err != nil {
    51  				return fmt.Errorf("init config with force=%v, err: %w", force, err)
    52  			}
    53  			// Do setup according to language.
    54  			if err := setup(lang); err != nil {
    55  				return fmt.Errorf("setup failed: %w", err)
    56  			}
    57  			log.Info("Setup completed")
    58  			return nil
    59  		},
    60  	}
    61  	setupCmd.Flags().StringArrayP("lang", "l", nil, "setup tools for languages")
    62  	setupCmd.Flags().BoolP("force", "f", false, "force extracting assets to overwrite the existing asset files")
    63  	return setupCmd
    64  }