/
versions.go
105 lines (83 loc) · 2.39 KB
/
versions.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2020 The appleopensource Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cmd
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"go-darwin.dev/appleopensource/pkg/appleopensource"
)
type versions struct {
*aos
ioStreams *IOStreams
product string
source bool
tarballs bool
}
// newCmdVersions creates the versions command.
func (a *aos) newCmdVersions(ctx context.Context, ioStreams *IOStreams) *cobra.Command {
versions := &versions{
aos: a,
ioStreams: ioStreams,
}
cmd := &cobra.Command{
Use: "versions product",
Short: "List all versions of the product.",
RunE: func(_ *cobra.Command, args []string) error {
versions.product = args[0]
return versions.runVersions(ctx)
},
}
f := cmd.Flags()
f.BoolVarP(&versions.source, "source", "s", false, "List the source resources type cache")
f.BoolVarP(&versions.tarballs, "tarballs", "t", false, "List the tarballs resources type cache")
return cmd
}
// index return the opensource.apple.com project index, and caches the HTML DOM tree into cacheDir.
func (v *versions) indexVersion(project string, typ appleopensource.ResourceType) ([]byte, error) {
versionsCachedir := filepath.Join(cacheDir(), typ.String())
if _, err := os.Stat(versionsCachedir); err != nil && errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(versionsCachedir, 0700); err != nil {
return nil, err
}
}
fname := filepath.Join(versionsCachedir, fmt.Sprintf("%s.html", project))
if _, err := os.Stat(fname); err == nil && !v.noCache {
return ioutil.ReadFile(fname)
}
buf, err := appleopensource.IndexVersion(project, typ)
if err != nil {
return nil, err
}
if err := ioutil.WriteFile(fname, buf, 0664); err != nil {
return nil, err
}
return buf, nil
}
func (v *versions) runVersions(ctx context.Context) error {
mode := appleopensource.TarballsResource
switch {
case v.tarballs:
// nothing to do
case v.source:
mode = appleopensource.SourceResource
case v.tarballs && v.source:
return errors.New("-source and -tarballs flags are must be one")
}
buf, err := v.indexVersion(v.product, mode)
if err != nil {
return err
}
list, err := appleopensource.ListVersions(buf)
if err != nil {
return err
}
_, err = fmt.Println(strings.Join(list, "\n"))
return err
}