This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Registry support #1172
Closed
Closed
Registry support #1172
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
24fd8e5
Registry support - download
df985d1
Registry support - download
1afea6b
Registry support - no-registry flag for ensure cmd
33d05a1
Registry support - publish
9d4e869
Registry support - publish
7926fd5
Registry support - gofmt and other review fixes
a79554a
Registry support - gofmt and other review fixes
b2ccf08
Registry support - few comment fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package main | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to hide this from normal builds until we're actually ready to ship the registry. feature-flag style, maybe we do this with build tags, or maybe something else, but we can't put new commands in front of our users until it's mostly baked. |
||
|
||
import ( | ||
"flag" | ||
"github.com/golang/dep" | ||
"github.com/pkg/errors" | ||
"fmt" | ||
"path" | ||
"net/http" | ||
"syscall" | ||
"golang.org/x/crypto/ssh/terminal" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/url" | ||
) | ||
|
||
const loginShortHelp = `Login to a registry server and save configuration` | ||
const loginLongHelp = ` | ||
Login to a remote registry server containing go dependencies and persist the login configuration. | ||
|
||
If your registry allows anonymous access you can leave out the username and password parameters. | ||
` | ||
|
||
const noValue = "depNoInputProvided" | ||
|
||
func (cmd *loginCommand) Name() string { return "login" } | ||
func (cmd *loginCommand) Args() string { return "<url>" } | ||
func (cmd *loginCommand) ShortHelp() string { return loginShortHelp } | ||
func (cmd *loginCommand) LongHelp() string { return loginLongHelp } | ||
func (cmd *loginCommand) Hidden() bool { return false } | ||
|
||
func (cmd *loginCommand) Register(fs *flag.FlagSet) { | ||
fs.StringVar(&cmd.user, "u", noValue, "provide username for registry") | ||
fs.StringVar(&cmd.password, "p", noValue, "provide password for registry") | ||
} | ||
|
||
type loginCommand struct { | ||
user string | ||
password string | ||
} | ||
|
||
func (cmd *loginCommand) getProject(ctx *dep.Ctx) (*dep.Project, error) { | ||
p, err := ctx.LoadProject() | ||
if p != nil { | ||
return p, err | ||
} | ||
p = new(dep.Project) | ||
if err := p.SetRoot(ctx.WorkingDir); err != nil { | ||
return nil, errors.Wrap(err, "NewProject") | ||
} | ||
return p, nil | ||
} | ||
|
||
func (cmd *loginCommand) Run(ctx *dep.Ctx, args []string) error { | ||
if len(args) > 1 { | ||
return errors.Errorf("too many args (%d)", len(args)) | ||
} | ||
|
||
if len(args) != 1 { | ||
return errors.New("registry URL is required") | ||
} | ||
|
||
p, err := cmd.getProject(ctx) | ||
u, err := url.Parse(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
u.Path = path.Join(u.Path, "api/v1/auth/token") | ||
|
||
var token string | ||
token, err = getToken(u.String(), cmd.user, cmd.password) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
p.RegistryConfig = dep.NewRegistryConfig(args[0], token) | ||
|
||
var sw *dep.SafeWriter | ||
sw, err = dep.NewSafeWriter(nil, nil, nil, p.RegistryConfig, dep.VendorNever) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := sw.Write(p.AbsRoot, nil, false, ctx.Err); err != nil { | ||
return errors.Wrap(err, "safe write of registry configuration") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func readUsername() (string, error) { | ||
var user string | ||
print("username: ") | ||
_, err := fmt.Scanln(&user) | ||
if err != nil { | ||
return "", err | ||
} | ||
return user, nil | ||
} | ||
|
||
func readPassword() (string, error) { | ||
print("password: ") | ||
password, err := terminal.ReadPassword(int(syscall.Stdin)) | ||
if err != nil { | ||
return "", err | ||
} | ||
println() | ||
return string(password), nil | ||
} | ||
|
||
func getToken(url, user, password string) (string, error) { | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if user == noValue { | ||
user, err = readUsername() | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
if password == noValue { | ||
password, err = readPassword() | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
req.SetBasicAuth(user, string(password)) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
return "", errors.Errorf("%s %s", url, http.StatusText(resp.StatusCode)) | ||
} | ||
var bytes []byte | ||
bytes, err = ioutil.ReadAll(resp.Body) | ||
|
||
var loginResp rawLoginResp | ||
err = json.Unmarshal(bytes, &loginResp) | ||
return loginResp.Token, err | ||
} | ||
|
||
type rawLoginResp struct { | ||
Token string `json:"token"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'm gonna focus several interrelated things on this one line:
dep status
(and possibly evendep init
)dep.Ctx
, which will obviate the need for the additional parameter onCtx.SourceManager()
.The only crucial thing here is that the value of the environment variable itself be injected onto the
dep.Ctx
(rather than being read directly from the environment out of anyCtx
method) so that tests can easily set their own values as needed.in keeping with some other notes in this review, we may also end up just forcing this to always be on, so that the registry-based code is not reachable at all. (or maybe there's another hookpoint we'll pick)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't having this as an environment variable defeat the purpose of ease of use? Practically speaking, 'no-registry' is something you wouldn't want to keep around as a global flag but merely use it to initiate vendor population.
If the reason is that this flag has to apply to other commands then surely there are ways to achieve that without requiring an environment variable.