Skip to content

Commit

Permalink
Merge pull request #1229 from apernet/wip-share
Browse files Browse the repository at this point in the history
feat: share subcommand
  • Loading branch information
tobyxdd authored Oct 19, 2024
2 parents 4567713 + 99e959f commit 78598bf
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions app/cmd/share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"fmt"

"github.com/apernet/hysteria/app/v2/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
)

var (
noText bool
withQR bool
)

// shareCmd represents the share command
var shareCmd = &cobra.Command{
Use: "share",
Short: "Generate share URI",
Long: "Generate a hysteria2:// URI from a client config for sharing",
Run: runShare,
}

func init() {
initShareFlags()
rootCmd.AddCommand(shareCmd)
}

func initShareFlags() {
shareCmd.Flags().BoolVar(&noText, "notext", false, "do not show URI as text")
shareCmd.Flags().BoolVar(&withQR, "qr", false, "show URI as QR code")
}

func runShare(cmd *cobra.Command, args []string) {
if err := viper.ReadInConfig(); err != nil {
logger.Fatal("failed to read client config", zap.Error(err))
}
var config clientConfig
if err := viper.Unmarshal(&config); err != nil {
logger.Fatal("failed to parse client config", zap.Error(err))
}
if _, err := config.Config(); err != nil {
logger.Fatal("failed to load client config", zap.Error(err))
}

u := config.URI()

if !noText {
fmt.Println(u)
}
if withQR {
utils.PrintQR(u)
}
}

0 comments on commit 78598bf

Please sign in to comment.