Add self-update, release 1.0.2
nscan update installs the newest release from git.micw.org, nscan update -c only checks; --update and --check-update work as well. The download lands next to the running binary, is started once with --version and only then replaces it. Once a day an ordinary run asks in the background and mentions a newer release in the title bar and on stderr, only on a terminal and not with NSCAN_NO_UPDATE_CHECK set. selfupdate.go is goca's copyable template. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
60257239d7
commit
c91dbe9f7d
11 files changed
+761
-16
No files matched your search
@@ -36,6 +36,7 @@ const usage = `Usage:
|
||||
nscan oui update download the vendor database from the IEEE
|
||||
nscan oui import <file> import oui.csv/oui.txt manually
|
||||
nscan oui lookup <mac> show the vendor of a MAC address
|
||||
nscan update [-c] install the newest release (-c: only check)
|
||||
nscan version print the version
|
||||
|
||||
Options for the interface and "scan":
|
||||
@@ -70,34 +71,63 @@ func run(args []string) error {
|
||||
args[0] = "version"
|
||||
case "-h", "-help", "--help":
|
||||
args[0] = "help"
|
||||
case "-update", "--update":
|
||||
args[0] = "update"
|
||||
case "-check-update", "--check-update":
|
||||
args = []string{"update", "-c"}
|
||||
case updateRefreshFlag: // the background look, deliberately not in the help
|
||||
selfUpdate.refresh()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
||||
cmd, args = args[0], args[1:]
|
||||
}
|
||||
var err error
|
||||
switch cmd {
|
||||
case "", "ui":
|
||||
return runUI(args)
|
||||
case "scan":
|
||||
return runScan(args)
|
||||
case "ping":
|
||||
return runPing(args)
|
||||
case "ports":
|
||||
return runPorts(args)
|
||||
case "wol":
|
||||
return runWOL(args)
|
||||
case "interfaces", "ifaces":
|
||||
return runInterfaces()
|
||||
case "oui":
|
||||
return runOUI(args)
|
||||
case "help":
|
||||
printUsage(os.Stdout)
|
||||
return nil
|
||||
case "version":
|
||||
fmt.Println("nscan " + Version)
|
||||
return nil
|
||||
case "update":
|
||||
return runUpdate(args)
|
||||
case "scan":
|
||||
err = runScan(args)
|
||||
case "ping":
|
||||
err = runPing(args)
|
||||
case "ports":
|
||||
err = runPorts(args)
|
||||
case "wol":
|
||||
err = runWOL(args)
|
||||
case "interfaces", "ifaces":
|
||||
err = runInterfaces()
|
||||
case "oui":
|
||||
err = runOUI(args)
|
||||
default:
|
||||
return fmt.Errorf("unknown command %q (see nscan help)", cmd)
|
||||
}
|
||||
return fmt.Errorf("unknown command %q (see nscan help)", cmd)
|
||||
// Costs nothing: the hint comes from a note in the cache, and the asking
|
||||
// happens at most once a day, in the background.
|
||||
if hint := selfUpdate.daily(); hint != "" {
|
||||
fmt.Fprintln(os.Stderr, hint)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func runUpdate(args []string) error {
|
||||
fl := newFlags("update")
|
||||
checkOnly := fl.Bool("c", false, "only check")
|
||||
if err := fl.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
if *checkOnly {
|
||||
return selfUpdate.check(os.Stdout)
|
||||
}
|
||||
return selfUpdate.install(os.Stdout)
|
||||
}
|
||||
|
||||
func newFlags(name string) *flag.FlagSet {
|
||||
@@ -173,6 +203,17 @@ func runUI(args []string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hint, newer := selfUpdate.daily(), ""
|
||||
if hint != "" {
|
||||
newer = selfUpdate.loadState().Latest
|
||||
}
|
||||
// The hint goes to the terminal again after the interface has closed,
|
||||
// where `nscan update` can be typed right away.
|
||||
defer func() {
|
||||
if hint != "" {
|
||||
fmt.Fprintln(os.Stderr, hint)
|
||||
}
|
||||
}()
|
||||
return tui.Run(tui.Config{
|
||||
Network: n,
|
||||
Range: spec,
|
||||
@@ -186,6 +227,7 @@ func runUI(args []string) error {
|
||||
OUI: db,
|
||||
ExportDir: *exportDir,
|
||||
Version: Version,
|
||||
Update: newer,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user