Skip to content

Commit

Permalink
feat: Profiles
Browse files Browse the repository at this point in the history
Add support for multiple configuration profiles
  • Loading branch information
coolapso committed Oct 26, 2024
1 parent e29a99f commit a148447
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 38 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ Megaphone allows you to post to multiple social networks simultaneously from you
## Features

* Configuration utility: use `megaphone configure` to set up the tool
* Multiple configuration profiles: use the `--profile` flag to setup and use different accounts
* Post to all supported social networks
* Post to all supported social networkds with images and videos
* Post only to X: use `megaphone -x "text"` to post only to X
* Post to Mastodon: use `megaphone -m "text"` to post only to Mastodon


## Supported social netrowks
* X
* Mastodon

### Planed features

* Threads
* Facebook (Still not very sure about this one)
* Threading, split longer texts and post them as threads
* Polls
* Polls for Mastodon and X

## Installation

Expand Down Expand Up @@ -75,7 +80,6 @@ These tokens can also be provided with the following environment variables:
> [!NOTE]
> You are subject to Twitter API pricing and limits. Please make sure to check the X developer portal information: https://developer.x.com/en

### Mastodon

* Mastodon configuration is all done through `megaphone configure`. During the process, it will open your browser and request you to paste the authorization code.
Expand All @@ -99,6 +103,7 @@ Flags:
-h, --help help for megophone
-m, --m-only Post to Mastodon Only
-p, --media-path string Path of media to be uploaded
--profile string The configuration profile to use (default "default")
-x, --x-only Post to X only
Use "megophone [command] --help" for more information about a command.
Expand Down
8 changes: 4 additions & 4 deletions cmd/config_mastodon.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func configMastodonServer(reader *bufio.Reader, c *config) {
viper.Set("mastodon_server", c.m.GetServer())
}

func registerMastodonApp(ctx context.Context, c *config) (*gomasto.Application, error) {
func registerMastodonApp(ctx context.Context, c *config, p string) (*gomasto.Application, error) {
appConfig := &gomasto.AppConfig{
Server: c.m.GetServer(),
ClientName: "megophone",
ClientName: fmt.Sprintf("megophone-%s", p),
Scopes: "read write follow",
Website: "https://github.com/coolapso/megophone",
RedirectURIs: redirectUri,
Expand Down Expand Up @@ -64,9 +64,9 @@ func mastodonClientConfig() *gomasto.Config {
}
}

func configMastodon(ctx context.Context, reader *bufio.Reader, c *config) error {
func configMastodon(ctx context.Context, reader *bufio.Reader, c *config, p string) error {
configMastodonServer(reader, c)
app, err := registerMastodonApp(ctx, c)
app, err := registerMastodonApp(ctx, c, p)
if err != nil {
return fmt.Errorf("Failed to register mastodon application %v\n", err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ type config struct {
func configMegophone(reader *bufio.Reader) error {
loadXVars(&c)
configX(reader, &c)
if err := configMastodon(context.Background(), reader, &c); err != nil {
if err := configMastodon(context.Background(), reader, &c, profile); err != nil {
return err
}

if err := writeConfigFile(); err != nil {
if err := writeConfigFile(profile); err != nil {
return err
}

return nil
}

func writeConfigFile() error {
func writeConfigFile(p string) error {
cfgDir, err := util.GetConfigDir()
if err != nil {
return fmt.Errorf("Failed to get config directory: %v", err.Error())
}

cfgFilePath, err := util.GetConfigFilePath()
cfgFilePath, err := util.GetConfigFilePath(p)
if err != nil {
return fmt.Errorf("Failed to get config file path: %v", err.Error())
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
)

func TestWriteConfigFile(t *testing.T) {
os.Setenv("GOLANG_TESTING", "true")
defer os.Unsetenv("GOLANG_TESTING")
profile := "megophone-test"

if err := writeConfigFile(); err != nil {
if err := writeConfigFile(profile); err != nil {
t.Fatal("Failed to write config file: ", err)
}

cfgFilePath, err := util.GetConfigFilePath()
cfgFilePath, err := util.GetConfigFilePath(profile)
if err != nil {
t.Fatal("Failed to get config file path: ", err)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

var (
cfgFile string
profile string
)

const (
Expand Down Expand Up @@ -192,6 +193,7 @@ func postAll(text, mediaPath string) (errors []error) {
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $XDG_HOME_CONFIG/megophone/config.yaml)")
rootCmd.PersistentFlags().StringVar(&profile, "profile", "default", "The configuration profile to use")
rootCmd.Flags().BoolP("x-only", "x", false, "Post to X only")
rootCmd.Flags().BoolP("m-only", "m", false, "Post to Mastodon Only")
rootCmd.Flags().StringP("media-path", "p", "", "Path of media to be uploaded")
Expand All @@ -208,7 +210,7 @@ func initConfig() {
cfgDir, err := util.GetConfigDir()
cobra.CheckErr(err)
viper.AddConfigPath(cfgDir)
viper.SetConfigName("megophone.env")
viper.SetConfigName(fmt.Sprintf("%s.env", profile))
viper.SetConfigType("env")
}

Expand Down
16 changes: 10 additions & 6 deletions internal/util/path.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package util

import (
"fmt"
"os"
"path/filepath"
)

// GetConfigDir returns the default directory to hold configuration profiles
func GetConfigDir() (string, error) {
userCfgDir, err := os.UserConfigDir()
if err != nil {
Expand All @@ -14,15 +16,17 @@ func GetConfigDir() (string, error) {
return filepath.Join(userCfgDir, "megophone"), nil
}

func GetConfigFilePath() (string, error) {
// GetConfigFileName returns the file name for a given profile
func GetConfigFileName(profile string) string {
return fmt.Sprintf("%s.env", profile)
}

// GetConfigFilePath eturns absolute path to the profile configuration file
func GetConfigFilePath(profile string) (string, error) {
cfgDir, err := GetConfigDir()
if err != nil {
return "", err
}

if os.Getenv("GOLANG_TESTING") == "true" {
return filepath.Join(cfgDir, "megophone-test.env"), nil
}

return filepath.Join(cfgDir, "megophone.env"), nil
return filepath.Join(cfgDir, GetConfigFileName(profile)), nil
}
28 changes: 11 additions & 17 deletions internal/util/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,25 @@ func TestGetConfigDir(t *testing.T) {
}
}

func TestGetconfigFileName(t *testing.T) {
want := "default.env"
got := GetConfigFileName("default")

if want != got {
t.Fatalf("Wrong filename, want %s, got %s", want, got)
}
}

func TestGetConfigFilePath(t *testing.T) {
cfgDir, err := os.UserConfigDir()
if err != nil {
t.Fatalf("Failed to get user config dir: %v", err)

}

t.Run("Test main fileppath", func(t *testing.T) {
want := filepath.Join(cfgDir, "megophone", "megophone.env")
got, err := GetConfigFilePath()
if err != nil {
t.Fatalf("Got error didn't expect one: %v", err)
}

if want != got {
t.Fatalf("Wrong file path: want %v, got %v", want, got)
}
})

t.Run("Test golang testing fileppath", func(t *testing.T) {
t.Run("Test default fileppath", func(t *testing.T) {
want := filepath.Join(cfgDir, "megophone", "megophone-test.env")
os.Setenv("GOLANG_TESTING", "true")
defer os.Unsetenv("GOLANG_TESTING")
got, err := GetConfigFilePath()
got, err := GetConfigFilePath("megophone-test")
if err != nil {
t.Fatalf("Got error didn't expect one: %v", err)
}
Expand All @@ -57,5 +52,4 @@ func TestGetConfigFilePath(t *testing.T) {
t.Fatalf("Wrong file path: want %v, got %v", want, got)
}
})

}

0 comments on commit a148447

Please sign in to comment.