-
Notifications
You must be signed in to change notification settings - Fork 41
/
googet_install.go
211 lines (193 loc) · 6.38 KB
/
googet_install.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
// The install subcommand handles the downloading and installation of a package.
import (
"bytes"
"context"
"flag"
"fmt"
"os"
"path/filepath"
"github.com/google/googet/v2/client"
"github.com/google/googet/v2/goolib"
"github.com/google/googet/v2/install"
"github.com/google/logger"
"github.com/google/subcommands"
)
type installCmd struct {
reinstall bool
redownload bool
dbOnly bool
sources string
}
func (*installCmd) Name() string { return "install" }
func (*installCmd) Synopsis() string { return "download and install a package and its dependencies" }
func (*installCmd) Usage() string {
return fmt.Sprintf("%s install [-reinstall] [-sources repo1,repo2...] <name>\n", filepath.Base(os.Args[0]))
}
func (cmd *installCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&cmd.reinstall, "reinstall", false, "install even if already installed")
f.BoolVar(&cmd.redownload, "redownload", false, "redownload package files")
f.BoolVar(&cmd.dbOnly, "db_only", false, "only make changes to DB, don't perform install system actions")
f.StringVar(&cmd.sources, "sources", "", "comma separated list of sources, setting this overrides local .repo files")
}
func (cmd *installCmd) Execute(ctx context.Context, flags *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if len(flags.Args()) == 0 {
fmt.Printf("%s\nUsage: %s\n", cmd.Synopsis(), cmd.Usage())
return subcommands.ExitFailure
}
if cmd.redownload && !cmd.reinstall {
fmt.Fprintln(os.Stderr, "It's an error to use the -redownload flag without the -reinstall flag")
return subcommands.ExitFailure
}
args := flags.Args()
exitCode := subcommands.ExitSuccess
cache := filepath.Join(rootDir, cacheDir)
sf := filepath.Join(rootDir, stateFile)
state, err := readState(sf)
if err != nil {
logger.Fatal(err)
}
if len(args) == 0 {
return exitCode
}
repos, err := buildSources(cmd.sources)
if err != nil {
logger.Fatal(err)
}
var rm client.RepoMap
for _, arg := range args {
if ext := filepath.Ext(arg); ext == ".goo" {
if !noConfirm {
if base := filepath.Base(arg); !confirmation(fmt.Sprintf("Install %s?", base)) {
fmt.Printf("Not installing %s...\n", base)
continue
}
}
if err := install.FromDisk(arg, cache, state, cmd.dbOnly, cmd.reinstall); err != nil {
logger.Errorf("Error installing %s: %v", arg, err)
exitCode = subcommands.ExitFailure
continue
}
if err := writeState(state, sf); err != nil {
logger.Fatalf("Error writing state file: %v", err)
}
continue
}
pi := goolib.PkgNameSplit(arg)
if cmd.reinstall {
if err := reinstall(ctx, pi, *state, cmd.redownload); err != nil {
logger.Errorf("Error reinstalling %s: %v", pi.Name, err)
exitCode = subcommands.ExitFailure
continue
}
if err := writeState(state, sf); err != nil {
logger.Fatalf("Error writing state file: %v", err)
}
continue
}
if len(rm) == 0 {
if repos == nil {
logger.Fatal("No repos defined, create a .repo file or pass using the -sources flag.")
}
rm = client.AvailableVersions(ctx, repos, filepath.Join(rootDir, cacheDir), cacheLife, proxyServer)
}
if pi.Ver == "" {
v, _, a, err := client.FindRepoLatest(pi, rm, archs)
pi.Ver, pi.Arch = v, a
if err != nil {
logger.Errorf("Can't resolve version for package %q: %v", pi.Name, err)
exitCode = subcommands.ExitFailure
continue
}
}
if _, err := goolib.ParseVersion(pi.Ver); err != nil {
logger.Errorf("Invalid package version %q: %v", pi.Ver, err)
exitCode = subcommands.ExitFailure
continue
}
r, err := client.WhatRepo(pi, rm)
if err != nil {
logger.Errorf("Error finding %s.%s.%s in repo: %v", pi.Name, pi.Arch, pi.Ver, err)
exitCode = subcommands.ExitFailure
continue
}
ni, err := install.NeedsInstallation(pi, *state)
if err != nil {
logger.Error(err)
exitCode = subcommands.ExitFailure
continue
}
if !ni {
fmt.Printf("%s.%s.%s or a newer version is already installed on the system\n", pi.Name, pi.Arch, pi.Ver)
continue
}
if !noConfirm {
b, err := enumerateDeps(pi, rm, r, archs, *state)
if err != nil {
logger.Error(err)
exitCode = subcommands.ExitFailure
continue
}
if !confirmation(b.String()) {
fmt.Println("canceling install...")
continue
}
}
if err := install.FromRepo(ctx, pi, r, cache, rm, archs, state, cmd.dbOnly, proxyServer); err != nil {
logger.Errorf("Error installing %s.%s.%s: %v", pi.Name, pi.Arch, pi.Ver, err)
exitCode = subcommands.ExitFailure
continue
}
if err := writeState(state, sf); err != nil {
logger.Fatalf("error writing state file: %v", err)
}
}
return exitCode
}
func reinstall(ctx context.Context, pi goolib.PackageInfo, state client.GooGetState, rd bool) error {
ps, err := state.GetPackageState(pi)
if err != nil {
return fmt.Errorf("cannot reinstall something that is not already installed")
}
if !noConfirm {
if !confirmation(fmt.Sprintf("Reinstall %s?", pi.Name)) {
fmt.Printf("Not reinstalling %s...\n", pi.Name)
return nil
}
}
if err := install.Reinstall(ctx, ps, state, rd, proxyServer); err != nil {
return fmt.Errorf("error reinstalling %s, %v", pi.Name, err)
}
return nil
}
func enumerateDeps(pi goolib.PackageInfo, rm client.RepoMap, r string, archs []string, state client.GooGetState) (*bytes.Buffer, error) {
dl, err := install.ListDeps(pi, rm, r, archs)
if err != nil {
return nil, fmt.Errorf("error listing dependencies for %s.%s.%s: %v", pi.Name, pi.Arch, pi.Ver, err)
}
var b bytes.Buffer
fmt.Fprintln(&b, "The following packages will be installed:")
for _, di := range dl {
ni, err := install.NeedsInstallation(di, state)
if err != nil {
return nil, err
}
if ni {
fmt.Fprintf(&b, " %s.%s.%s\n", di.Name, di.Arch, di.Ver)
}
}
fmt.Fprintf(&b, "Do you wish to install %s.%s.%s and all dependencies?", pi.Name, pi.Arch, pi.Ver)
return &b, nil
}