Skip to content

Commit

Permalink
add password support for garmin
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Carl <[email protected]>
  • Loading branch information
grounded042 committed Apr 21, 2018
1 parent 66e6544 commit f5be876
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions garmin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func LoadFile(filepath string) (*Kml, error) {

// LoadURL loads data from the specified URL and returns the Kml object with
// the data
func LoadURL(url string) (*Kml, error) {
feed, err := util.GetURLBody(url)
func LoadURL(url, password string) (*Kml, error) {
feed, err := util.GetURLBody(url, password)

if err != nil {
return nil, err
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var sourceURL string
var startDate string
var endDate string
var tripName string
var password string

const timestampFormat = "01/02/2006"
const tripsFolder = "trips/"
Expand All @@ -37,6 +38,7 @@ func main() {
rootCmd.Flags().StringVarP(&startDate, "start_date", "d", "", "The day on which to start pulling data from in the format of <month>/<day>/<year> with leading zeros")
rootCmd.Flags().StringVarP(&endDate, "end_date", "e", "", "The day on which to stop pulling data from in the format of <month>/<day>/<year> with leading zeros")
rootCmd.Flags().StringVarP(&tripName, "trip_name", "n", "", "The name of the trip")
rootCmd.Flags().StringVarP(&password, "password", "p", "", "The username, if any, for pulling the data from the source_url")

rootCmd.Run = func(cmd *cobra.Command, args []string) {
timeStartDate := convertDate(startDate, "`start_date` was not supplied - will not filter based on a start date")
Expand All @@ -46,7 +48,7 @@ func main() {
timeEndDate = timeEndDate.AddDate(0, 0, 1)
}

translate(source, sourceURL, timeStartDate, timeEndDate, tripName)
translate(source, sourceURL, timeStartDate, timeEndDate, tripName, password)
}

err := rootCmd.Execute()
Expand Down Expand Up @@ -149,13 +151,13 @@ type generateFrom interface {
GetAllPoints(time.Time, time.Time) []models.Point
}

func translate(tSource, url string, sDate, eDate time.Time, tName string) {
func translate(tSource, url string, sDate, eDate time.Time, tName, pword string) {
var gFrom generateFrom
var err error

switch strings.ToLower(tSource) {
case "garmin":
gFrom, err = garmin.LoadURL(url)
gFrom, err = garmin.LoadURL(url, pword)
default:
panic(fmt.Sprintf("unknown source: %v", source))
}
Expand Down
13 changes: 11 additions & 2 deletions util/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ import (
)

// GetURLBody gets and returns the body of the passed in url
func GetURLBody(url string) ([]byte, error) {
resp, err := http.Get(url)
func GetURLBody(url, password string) ([]byte, error) {
client := &http.Client{}

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("Error building GET: %v", err)
}

req.SetBasicAuth("", password)

resp, err := client.Do(req)

if err != nil {
return nil, fmt.Errorf("GET error: %v", err)
Expand Down

0 comments on commit f5be876

Please sign in to comment.