github.com/defang-io/defang/src@v0.0.0-20240505002154-bdf411911834/cmd/cli/command/hint.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/defang-io/defang/src/pkg"
    11  )
    12  
    13  func prettyExecutable(def string) string {
    14  	if os.Args[0] == def {
    15  		return def
    16  	}
    17  	executable, _ := os.Executable()
    18  	if executable == "" || strings.HasPrefix(executable, os.TempDir()) {
    19  		// If the binary is from the temp folder, default to def
    20  		return def
    21  	}
    22  	wd, err := os.Getwd()
    23  	if err != nil {
    24  		return def
    25  	}
    26  	executable, _ = filepath.Rel(wd, executable)
    27  	if executable == def {
    28  		executable = "./" + def // to ensure it's executable
    29  	}
    30  	if executable == "" {
    31  		return def
    32  	}
    33  	return executable
    34  }
    35  
    36  func printDefangHint(hint, args string) {
    37  	if pkg.GetenvBool("DEFANG_HIDE_HINTS") || !hasTty {
    38  		return
    39  	}
    40  
    41  	executable := prettyExecutable("defang")
    42  
    43  	fmt.Printf("\n%s\n", hint)
    44  	providerFlag := RootCmd.Flag("provider")
    45  	clusterFlag := RootCmd.Flag("cluster")
    46  	if providerFlag.Changed {
    47  		fmt.Printf("\n  %s --provider %s %s\n\n", executable, providerFlag.Value.String(), args)
    48  	} else if clusterFlag.Changed {
    49  		fmt.Printf("\n  %s --cluster %s %s\n\n", executable, clusterFlag.Value.String(), args)
    50  	} else {
    51  		fmt.Printf("\n  %s %s\n\n", executable, args)
    52  	}
    53  	if rand.Intn(10) == 0 {
    54  		fmt.Println("To silence these hints, do: export DEFANG_HIDE_HINTS=1")
    55  	}
    56  }