Skip to content

Commit

Permalink
- Add support for environment-aware config file.
Browse files Browse the repository at this point in the history
- Fix an issue where tasks would not be fetched if there was only one
  page.
  • Loading branch information
Scott Jackson committed May 23, 2023
1 parent e02b9cc commit d80912a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
58 changes: 45 additions & 13 deletions bb2todotxt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package main

import (
"encoding/json"
"errors"
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"

"github.com/kirsle/configdir"
)

const VERSION = "v0.0.3"
Expand All @@ -19,6 +21,40 @@ type bitbucketConfig struct {
Password string `json:password`
}

const PACKAGE_NAME = "com.scottmmjackson.bb2todotxt"

var WELL_KNOWN_CONFIG_FILE_PATH = filepath.Join(configdir.LocalConfig(PACKAGE_NAME), "bitbucket.json")

func resolveConfigFile(commandLinePath string) (*bitbucketConfig, error) {
var err error
var bitbucketConfigString []byte
var bitbucketConfigMap *bitbucketConfig
if commandLinePath != "" {
_, err = os.Stat(commandLinePath)
if err != nil {
return nil, fmt.Errorf("Unable to open config file at %s: %s", WELL_KNOWN_CONFIG_FILE_PATH, err)
}
bitbucketConfigString, err = os.ReadFile(commandLinePath)
if err != nil {
return nil, fmt.Errorf("Unable to upen config file at %s: %s", commandLinePath, err)
}
} else {
_, err = os.Stat(WELL_KNOWN_CONFIG_FILE_PATH)
if err != nil {
return nil, fmt.Errorf("Unable to open config file at %s: %s", WELL_KNOWN_CONFIG_FILE_PATH, err)
}
bitbucketConfigString, err = os.ReadFile(WELL_KNOWN_CONFIG_FILE_PATH)
if err != nil {
return nil, fmt.Errorf("Unable to open config file at %s: %s", WELL_KNOWN_CONFIG_FILE_PATH, err)
}
}
err = json.Unmarshal(bitbucketConfigString, &bitbucketConfigMap)
if err != nil {
return nil, err
}
return bitbucketConfigMap, nil
}

func commandLine() (*bitbucketConfig, string, string, int, error) {
var bitbucketConfigMap *bitbucketConfig
version := flag.Bool("v", false, "print version and quit")
Expand All @@ -31,16 +67,9 @@ func commandLine() (*bitbucketConfig, string, string, int, error) {
fmt.Println(VERSION)
os.Exit(0)
}
if *bitbucketConfigFile == "" {
return bitbucketConfigMap, "", "", 0, errors.New("Required flags not provided.")
}
bitbucketConfigString, err := os.ReadFile(*bitbucketConfigFile)
bitbucketConfigMap, err := resolveConfigFile(*bitbucketConfigFile)
if err != nil {
return bitbucketConfigMap, "", "", 0, err
}
err = json.Unmarshal(bitbucketConfigString, &bitbucketConfigMap)
if err != nil {
return bitbucketConfigMap, "", "", 0, err
return nil, "", "", 0, err
}
return bitbucketConfigMap, *slug, *owner, *id, nil
}
Expand Down Expand Up @@ -117,6 +146,9 @@ func getTasks(bucketConfig *bitbucketConfig, uri string) ([]Task, string) {
log.Fatalf("error: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode > 300 {
log.Fatalf("error: %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("error: %s", err)
Expand All @@ -143,10 +175,10 @@ func main() {
BITBUCKET_URL,
owner, slug, id,
)
taskChunk, next := getTasks(bitbucketConfig, uri)
for next != "" {
var taskChunk []Task
for uri != "" {
taskChunk, uri = getTasks(bitbucketConfig, uri)
tasks = append(tasks, taskChunk...)
taskChunk, next = getTasks(bitbucketConfig, next)
}
todoTmpl.Execute(os.Stdout, tasks)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/scottmmjackson/bb2todotxt

go 1.20

require github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=

0 comments on commit d80912a

Please sign in to comment.