mirror of
				https://github.com/sigcn/pg.git
				synced 2025-11-01 04:13:18 +08:00 
			
		
		
		
	cmd/pgcli: add func printVersion
This commit is contained in:
		| @@ -27,7 +27,7 @@ func Run() error { | |||||||
|  |  | ||||||
| func parseSecretKeyAndServer(flagSet *flag.FlagSet, args []string) (secretKey string, server string, err error) { | func parseSecretKeyAndServer(flagSet *flag.FlagSet, args []string) (secretKey string, server string, err error) { | ||||||
| 	flagSet.StringVar(&secretKey, "secret-key", "", "key to generate network secret") | 	flagSet.StringVar(&secretKey, "secret-key", "", "key to generate network secret") | ||||||
| 	flagSet.StringVar(&secretKey, "server", "", "peermap server url") | 	flagSet.StringVar(&secretKey, "s", "", "peermap server url") | ||||||
| 	flagSet.Parse(args) | 	flagSet.Parse(args) | ||||||
|  |  | ||||||
| 	if secretKey == "" { | 	if secretKey == "" { | ||||||
|   | |||||||
| @@ -27,7 +27,7 @@ func Run() error { | |||||||
| 	} | 	} | ||||||
| 	downloader := fileshare.Downloader{ListenUDPPort: 28879} | 	downloader := fileshare.Downloader{ListenUDPPort: 28879} | ||||||
|  |  | ||||||
| 	flagSet.StringVar(&downloader.Server, "server", "", "peermap server") | 	flagSet.StringVar(&downloader.Server, "s", "", "peermap server") | ||||||
| 	flagSet.StringVar(&downloader.Network, "pubnet", "public", "peermap public network") | 	flagSet.StringVar(&downloader.Network, "pubnet", "public", "peermap public network") | ||||||
|  |  | ||||||
| 	var logLevel int | 	var logLevel int | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ import ( | |||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"log/slog" | 	"log/slog" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"runtime/debug" | ||||||
|  |  | ||||||
| 	"github.com/sigcn/pg/cmd/pgcli/admin" | 	"github.com/sigcn/pg/cmd/pgcli/admin" | ||||||
| 	"github.com/sigcn/pg/cmd/pgcli/curve25519" | 	"github.com/sigcn/pg/cmd/pgcli/curve25519" | ||||||
| @@ -21,6 +22,7 @@ func main() { | |||||||
| 	var logLevel int | 	var logLevel int | ||||||
| 	flag.Usage = usage | 	flag.Usage = usage | ||||||
| 	flag.IntVar(&logLevel, "loglevel", 0, "log level") | 	flag.IntVar(&logLevel, "loglevel", 0, "log level") | ||||||
|  | 	flag.BoolFunc("v", "print binary version", printVersion) | ||||||
| 	flag.Parse() | 	flag.Parse() | ||||||
| 	slog.SetLogLoggerLevel(slog.Level(logLevel)) | 	slog.SetLogLoggerLevel(slog.Level(logLevel)) | ||||||
| 	if err := run(); err != nil { | 	if err := run(); err != nil { | ||||||
| @@ -40,7 +42,12 @@ func run() error { | |||||||
| 	case "share": | 	case "share": | ||||||
| 		return share.Run() | 		return share.Run() | ||||||
| 	case "vpn": | 	case "vpn": | ||||||
|  | 		commit, _, _ := readBinaryInfo() | ||||||
|  | 		if len(commit) < 7 { | ||||||
| 			vpn.Version = Version | 			vpn.Version = Version | ||||||
|  | 		} else { | ||||||
|  | 			vpn.Version = fmt.Sprintf("%s-%s", Version, commit[:7]) | ||||||
|  | 		} | ||||||
| 		return vpn.Run() | 		return vpn.Run() | ||||||
| 	default: | 	default: | ||||||
| 		usage() | 		usage() | ||||||
| @@ -58,6 +65,32 @@ func usage() { | |||||||
| 	fmt.Printf("  share\t\tShare files to peers\n") | 	fmt.Printf("  share\t\tShare files to peers\n") | ||||||
| 	fmt.Printf("  vpn\t\tRun a vpn daemon which backend is PeerGuard p2p network\n\n") | 	fmt.Printf("  vpn\t\tRun a vpn daemon which backend is PeerGuard p2p network\n\n") | ||||||
| 	fmt.Printf("Flags:\n") | 	fmt.Printf("Flags:\n") | ||||||
| 	fmt.Printf("  --loglevel\n\tlog level\n") |  | ||||||
| 	fmt.Printf("  -v, --version\n\tprint binary version\n") | 	fmt.Printf("  -v, --version\n\tprint binary version\n") | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func readBinaryInfo() (commit, buildTime, goVersion string) { | ||||||
|  | 	info, ok := debug.ReadBuildInfo() | ||||||
|  | 	goVersion = info.GoVersion | ||||||
|  | 	if ok { | ||||||
|  | 		for _, kv := range info.Settings { | ||||||
|  | 			if kv.Key == "vcs.revision" { | ||||||
|  | 				commit = kv.Value | ||||||
|  | 				continue | ||||||
|  | 			} | ||||||
|  | 			if kv.Key == "vcs.time" { | ||||||
|  | 				buildTime = kv.Value | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func printVersion(string) error { | ||||||
|  | 	commit, buildTime, goVersion := readBinaryInfo() | ||||||
|  | 	fmt.Println(goVersion) | ||||||
|  | 	fmt.Printf("version\t\t%s\n", Version) | ||||||
|  | 	fmt.Printf("build_time\t%s\n", buildTime) | ||||||
|  | 	fmt.Printf("commit\t\t%s\n", commit) | ||||||
|  | 	os.Exit(0) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|   | |||||||
| @@ -24,7 +24,7 @@ func Run() error { | |||||||
| 	} | 	} | ||||||
| 	fileManager := fileshare.FileManager{ListenUDPPort: 28878, ProgressBar: createBar} | 	fileManager := fileshare.FileManager{ListenUDPPort: 28878, ProgressBar: createBar} | ||||||
|  |  | ||||||
| 	flagSet.StringVar(&fileManager.Server, "server", "", "peermap server") | 	flagSet.StringVar(&fileManager.Server, "s", "", "peermap server") | ||||||
| 	flagSet.StringVar(&fileManager.Network, "pubnet", "public", "peermap public network") | 	flagSet.StringVar(&fileManager.Network, "pubnet", "public", "peermap public network") | ||||||
| 	flagSet.StringVar(&fileManager.PrivateKey, "key", "", "curve25519 private key in base58 format (default generate a new one)") | 	flagSet.StringVar(&fileManager.PrivateKey, "key", "", "curve25519 private key in base58 format (default generate a new one)") | ||||||
|  |  | ||||||
|   | |||||||
| @@ -52,6 +52,7 @@ func Run() error { | |||||||
| 	var logLevel int | 	var logLevel int | ||||||
| 	flagSet.BoolVar(&pprof, "pprof", false, "enable http pprof server") | 	flagSet.BoolVar(&pprof, "pprof", false, "enable http pprof server") | ||||||
| 	flagSet.IntVar(&logLevel, "loglevel", 0, "log level") | 	flagSet.IntVar(&logLevel, "loglevel", 0, "log level") | ||||||
|  | 	flagSet.IntVar(&logLevel, "V", 0, "") | ||||||
| 	cfg, err := createConfig(flagSet, flag.Args()[1:]) | 	cfg, err := createConfig(flagSet, flag.Args()[1:]) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 rkonfj
					rkonfj