github.com/creativeprojects/go-selfupdate@v1.2.0/cmd/detect-latest-release/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"log"
     8  	"os"
     9  
    10  	"github.com/creativeprojects/go-selfupdate"
    11  	"github.com/creativeprojects/go-selfupdate/cmd"
    12  )
    13  
    14  const (
    15  	usageBloc = `
    16  Usage: detect-latest-release [flags] {repository}
    17  
    18    {repository} can be:
    19      - URL to a repository
    20      - "owner/repository_name" couple separated by a "/"
    21      - numeric ID for Gitlab only
    22  
    23  `
    24  )
    25  
    26  func usage() {
    27  	fmt.Fprint(os.Stderr, usageBloc, "Flags:\n")
    28  	flag.PrintDefaults()
    29  }
    30  
    31  func main() {
    32  	var help, verbose bool
    33  	var cvsType, forceOS, forceArch string
    34  	flag.BoolVar(&help, "h", false, "Show help")
    35  	flag.BoolVar(&verbose, "v", false, "Display debugging information")
    36  	flag.StringVar(&cvsType, "t", "auto", "Version control: \"github\", \"gitea\" or \"gitlab\"")
    37  	flag.StringVar(&forceOS, "o", "", "OS name to use (windows, darwin, linux, etc)")
    38  	flag.StringVar(&forceArch, "a", "", "CPU architecture to use (amd64, arm64, etc)")
    39  
    40  	flag.Usage = usage
    41  	flag.Parse()
    42  
    43  	if help || flag.NArg() != 1 {
    44  		usage()
    45  		return
    46  	}
    47  
    48  	if verbose {
    49  		selfupdate.SetLogger(log.New(os.Stdout, "", 0))
    50  	}
    51  
    52  	repo := flag.Arg(0)
    53  
    54  	domain, slug, err := cmd.SplitDomainSlug(repo)
    55  	if err != nil {
    56  		fmt.Fprintln(os.Stderr, err)
    57  		os.Exit(1)
    58  	}
    59  
    60  	if verbose {
    61  		fmt.Printf("slug %q on domain %q\n", slug, domain)
    62  	}
    63  
    64  	source, err := cmd.GetSource(cvsType, domain)
    65  	if err != nil {
    66  		fmt.Fprintln(os.Stderr, err)
    67  		os.Exit(1)
    68  	}
    69  
    70  	cfg := selfupdate.Config{
    71  		Source: source,
    72  	}
    73  	if forceOS != "" {
    74  		cfg.OS = forceOS
    75  	}
    76  	if forceArch != "" {
    77  		cfg.Arch = forceArch
    78  	}
    79  	updater, err := selfupdate.NewUpdater(cfg)
    80  	if err != nil {
    81  		fmt.Fprintln(os.Stderr, err)
    82  		os.Exit(1)
    83  	}
    84  
    85  	latest, found, err := updater.DetectLatest(context.Background(), selfupdate.ParseSlug(slug))
    86  	if err != nil {
    87  		fmt.Fprintln(os.Stderr, err)
    88  		os.Exit(1)
    89  	}
    90  	if !found {
    91  		fmt.Println("No release found")
    92  		return
    93  	}
    94  	fmt.Printf("Latest version: %s\n", latest.Version())
    95  	fmt.Printf("Download URL:   %q\n", latest.AssetURL)
    96  	fmt.Printf("Release URL:    %q\n", latest.URL)
    97  	fmt.Printf("Release Notes:\n%s\n", latest.ReleaseNotes)
    98  }