-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
735baaf
commit 0f357e6
Showing
4 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
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,47 @@ | ||
package client | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/spf13/afero" | ||
) | ||
|
||
type Config struct { | ||
URL string `flag:"url,Proxy URL" reg:"URL"` | ||
KeyId string `flag:"keyid,Access Key ID" reg:"KeyID"` | ||
KeySecret string `flag:"secret,Access Key Secret" reg:"KeySecret"` | ||
} | ||
|
||
func (c *Config) Validate() error { | ||
if c.URL == "" { | ||
return errors.New("url is mandatory") | ||
} | ||
if c.KeyId == "" { | ||
return errors.New("keyid is mandatory") | ||
} | ||
if c.KeySecret == "" { | ||
return errors.New("secret is mandatory") | ||
} | ||
return nil | ||
} | ||
|
||
type authenticator struct { | ||
*Config | ||
delegate http.RoundTripper | ||
} | ||
|
||
func (a *authenticator) RoundTrip(r *http.Request) (*http.Response, error) { | ||
r.Header.Add("X-Key-ID", a.KeyId) | ||
r.Header.Add("X-Secret", a.KeySecret) | ||
return a.delegate.RoundTrip(r) | ||
} | ||
|
||
func (c *Config) ToFileSystem() (afero.Fs, error) { | ||
httpclient := &http.Client{} | ||
httpclient.Transport = &authenticator{ | ||
Config: c, | ||
delegate: http.DefaultTransport, | ||
} | ||
return Connect(c.URL, httpclient) | ||
} |
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