diff --git a/.woodpecker/build.yml b/.woodpecker/build.yml index ff3bf70..e7d07c2 100644 --- a/.woodpecker/build.yml +++ b/.woodpecker/build.yml @@ -26,7 +26,7 @@ pipeline: - branch: release-* commands: - cd cmd - # - ./tests.sh + - ./tests.sh webserver_test: image: offline-twitter/go @@ -38,14 +38,6 @@ pipeline: - curl localhost:1000/cernovich > webserver_test_output.html - jobs -p | xargs -I{} kill -- -{} - windows_build: - image: offline-twitter/go-mingw - commands: - - cd cmd - # - export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^release-)\d+\.\d+\.\d+") - - export version=60.43.53 - - ./windows-compile.sh $version - version_bump_test: image: offline-twitter/go when: @@ -79,11 +71,21 @@ pipeline: - scp offline-twitter-engine_$${version}_all.deb aptrepo@apt.playfulpachyderm.com:/apt-repo/test-repo - ssh aptrepo@apt.playfulpachyderm.com "cd ~/test-repo && ./update.sh" + windows_build: + image: offline-twitter/go-mingw + when: + branch: release-* + commands: + - cd cmd + - export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^release-)\d+\.\d+\.\d+") + - ./windows-compile.sh $version + windows_package: + when: + branch: release-* image: offline-twitter/innosetup commands: - # - export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^release-)\d+\.\d+\.\d+") - - export version=60.43.53 + - export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^release-)\d+\.\d+\.\d+") - iscc.sh /Dversion=$version /Dexe_path=`winepath -w cmd/twitter.exe` build/windows/setup.iss diff --git a/cmd/twitter/helpers.go b/cmd/twitter/helpers.go index 9921635..b62feb6 100644 --- a/cmd/twitter/helpers.go +++ b/cmd/twitter/helpers.go @@ -4,6 +4,8 @@ import ( _ "embed" "fmt" "os" + "path/filepath" + "runtime" "strconv" "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper" @@ -56,3 +58,27 @@ func extract_id_from(url string) (scraper.TweetID, error) { num, err := strconv.Atoi(url) return scraper.TweetID(num), err } + +// Get a sensible default path to create a default profile. Uses `XDG_DATA_HOME` if available +// +// Defaults: +// - Unix: `~/.local/share` +// - Windows: %APPDATA% +// - MacOS: ~/Library +func get_default_profile() string { + app_data_dir := os.Getenv("XDG_DATA_HOME") + if app_data_dir == "" { + switch runtime.GOOS { + case "windows": + app_data_dir = os.Getenv("AppData") + if app_data_dir == "" { + panic("%AppData% is undefined") + } + case "darwin": + app_data_dir = filepath.Join(os.Getenv("HOME"), "Library") + default: // Unix + app_data_dir = filepath.Join(os.Getenv("HOME"), ".local", "share") + } + } + return filepath.Join(app_data_dir, "twitter") +} diff --git a/cmd/twitter/main.go b/cmd/twitter/main.go index 5a5a676..192b3c1 100644 --- a/cmd/twitter/main.go +++ b/cmd/twitter/main.go @@ -5,6 +5,8 @@ import ( "fmt" log "github.com/sirupsen/logrus" "golang.org/x/term" + "errors" + "io/fs" "os" "strconv" "strings" @@ -24,6 +26,8 @@ func main() { profile_dir := flag.String("profile", ".", "") flag.StringVar(profile_dir, "p", ".", "") + use_default_profile := flag.Bool("default-profile", false, "") + show_version_flag := flag.Bool("version", false, "") flag.BoolVar(show_version_flag, "v", false, "") @@ -88,9 +92,33 @@ func main() { return } + if *use_default_profile { + if *profile_dir != "." { + die(fmt.Sprintf("Invalid flags: either `--profile [...]` or `--default-profile` can be used, but not both"), true, 2) + } + *profile_dir = get_default_profile() + + // Create default profile if necessary + fileinfo, err := os.Stat(*profile_dir) + if errors.Is(err, fs.ErrNotExist) { + // Doesn't exist; create it + create_profile(*profile_dir) + } else if err != nil { + // Unexpected error + die(fmt.Sprintf("Default profile path (%s) is weird: %s", *profile_dir, err.Error()), false, 2) + } else if !fileinfo.IsDir() { + // It exists but it's not a directory + die(fmt.Sprintf("Default profile path (%s) already exists and is not a directory", *profile_dir), false, 2) + } + // Path exists and is a directory; safe to continue + } profile, err = persistence.LoadProfile(*profile_dir) if err != nil { - die(fmt.Sprintf("Could not load profile: %s", err.Error()), true, 2) + if *use_default_profile { + create_profile(*profile_dir) + } else { + die(fmt.Sprintf("Could not load profile: %s", err.Error()), true, 2) + } } if *session_name != "" { @@ -105,8 +133,6 @@ func main() { } switch operation { - case "create_profile": - create_profile(target) case "login": var password string if len(args) == 2 {