This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
diff.go
84 lines (68 loc) · 1.66 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"os"
"github.com/go2c/optparse"
"github.com/onodera-punpun/prt/ports"
)
// DiffCommand lists outdated packages.
func diffCommand(input []string) error {
// Define valid arguments.
o := optparse.New()
argn := o.Bool("no-alias", 'n', false)
argv := o.Bool("version", 'v', false)
argh := o.Bool("help", 'h', false)
// Parse arguments.
_, err := o.Parse(input)
if err != nil {
return fmt.Errorf("invaild argument, use `-h` for a list of arguments")
}
// Print help.
if *argh {
fmt.Println("Usage: prt diff [arguments]")
fmt.Println("")
fmt.Println("arguments:")
fmt.Println(" -n, --no-alias disable aliasing")
fmt.Println(" -v, --version print with version info")
fmt.Println(" -h, --help print help and exit")
return nil
}
// Get all ports.
all, err := ports.All()
if err != nil {
return err
}
// Get installed ports.
var db ports.Database
if err := db.Parse(); err != nil {
return err
}
for _, n := range db.Packages {
pl, err := ports.Locate(all, n.Name)
if err != nil {
continue
}
p := pl[0]
// Alias if needed.
if !*argn {
p.Alias()
}
// Read out Pkgfile.
if err := p.Pkgfile.Parse(); err != nil {
fmt.Fprintf(os.Stderr, "%s%s\n", warning(config.WarningChar), err)
continue
}
// Get available version and release from Pkgfile.
v := p.Pkgfile.Version + "-" + p.Pkgfile.Release
// Print if installed and available version don't match.
if v != n.Version {
fmt.Print(p.Pkgfile.Name)
// Print version information if needed.
if *argv {
fmt.Printf(" %s%s%s", n.Version, dark(" -> "), v)
}
fmt.Println()
}
}
return nil
}