link

package
v0.22.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LinkCmd = &cobra.Command{
	Use:   "link <long_url>",
	Short: "Create a shortened URL",
	Long: `
The link command creates a shortened URL for a given long URL.

Usage:
  hyphen link <long_url> [flags]

This command allows you to:
- Generate a short URL for a given long URL
- Optionally create a QR code for the shortened URL
- Specify a custom domain, tags, custom short code, and title
- Retrieve the shortened URL and optional QR code URL

The command will automatically add 'https://' to the long URL if not present.

If no custom domain is specified, the command will use the first ready domain 
associated with your organization.

Examples:
  hyphen link example.com
  hyphen link https://very-long-url.com --qr
  hyphen link example.com --tag promo --tag summer2023 --code summer-sale
  hyphen link example.com --title "Summer Sale" --domain custom.short.link

Use 'hyphen link --help' for more information about available flags.
`,
	Args: cobra.ExactArgs(1),
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		return user.ErrorIfNotAuthenticated()
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		printer = cprint.NewCPrinter(flags.VerboseFlag)
		service := newService(zelda.NewService())

		orgId, err := flags.GetOrganizationID()
		if err != nil {
			return fmt.Errorf("failed to get organization ID: %w", err)
		}

		if flags.VerboseFlag {
			printer.Info("Fetching domain information...")
		}

		domain, err := service.GetDomain(orgId)
		if err != nil {
			return fmt.Errorf("failed to get domain: %w", err)
		}

		if flags.VerboseFlag {
			printer.Success(fmt.Sprintf("Using domain: %s", domain))
		}

		var codePtr, titlePtr *string
		if code != "" {
			codePtr = &code
		}
		if title != "" {
			titlePtr = &title
		}

		longURL := args[0]

		if longURL[:8] != "https://" {
			longURL = "https://" + longURL
		}

		newCode := zelda.Code{
			LongURL:        longURL,
			Domain:         domain,
			Code:           codePtr,
			Title:          titlePtr,
			Tags:           tags,
			OrganizationID: orgId,
		}

		if flags.VerboseFlag {
			printer.Info("Generating short code...")
		}

		shortCode, err := service.GenerateShortCode(orgId, newCode)
		if err != nil {
			return fmt.Errorf("failed to generate short code: %w", err)
		}

		var qrCodeURL string
		if qr == true {
			if flags.VerboseFlag {
				printer.Info("Generating QR code...")
			}
			qrCode, err := service.GenerateQR(orgId, *shortCode.ID)
			if err != nil {
				return fmt.Errorf("failed to generate QR code: %w", err)
			}
			qrCodeURL = qrCode.QRLink
		}

		if flags.VerboseFlag {
			printer.Success("Link generation successful")
		}

		shortURL := fmt.Sprintf("%s/%s", domain, *shortCode.Code)

		if flags.VerboseFlag {
			printer.PrintDetail("Long URL", args[0])
			printer.PrintDetail("Short URL", shortURL)
			printer.PrintDetail("Short Code", *shortCode.Code)
			if shortCode.Title != nil {
				printer.PrintDetail("Title", *shortCode.Title)
			}
			if len(tags) > 0 {
				printer.PrintDetail("Tags", fmt.Sprintf("%v", tags))
			}
			if qrCodeURL != "" {
				printer.PrintDetail("QR Code URL", qrCodeURL)
			}

			printer.GreenPrint("\nURL shortened successfully!")

			if qr {
				printer.PrintHeader("QR Code")
				printer.PrintNorm(shortURL)
			}
		} else {
			printer.Print(shortURL)
			if qr {
				printer.Print(qrCodeURL)
			}
		}
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL