Skip to content

Commit

Permalink
refactor!: move platforms to internal/platforms/
Browse files Browse the repository at this point in the history
  • Loading branch information
slashtechno authored Jun 20, 2024
1 parent 4dad1b1 commit e5e1d5d
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 184 deletions.
103 changes: 32 additions & 71 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/charmbracelet/log"
"github.com/slashtechno/cross-blogger/internal/platforms"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -24,81 +25,41 @@ var publishCmd = &cobra.Command{
log.Fatal("Failed to get destinations from config")
}

// Make a list of the Destination structs if the destination name is in the args
var destinationSlice []Destination
// _ ignores the index. `dest` is the map
for _, dest := range destinations.([]interface{}) {
destMap, ok := dest.(map[string]interface{})
if !ok {
log.Fatal("Failed to convert destination to map")
}
destination, err := CreateDestination(destMap)
if err != nil {
log.Fatal(err)
}
// Check if the destination name is in the third argument or onwards
for _, arg := range args[2:] {
if destination.GetName() == arg {
log.Info("Adding destination", "destination", destination.GetName())
destinationSlice = append(destinationSlice, destination)
} else {
log.Info("Not adding destination as it isn't specified in args", "destination", destination.GetName())
}
}
// Load the sources and destinations
// The slice of selected destinations should just be the first argument (0) as a slice
sourceSlice, destinationSlice, err := platforms.Load(sources, destinations, []string{args[0]}, args[2:])
if err != nil {
log.Fatal(err)
}
log.Debug("Destination slice", "destinations", destinationSlice)
// Create a list of all the sources. If the source name is the first arg, use that source
var source Source
// Whilst this shouldn't happen since args[0] is passed to Load, iterate over the sources to ensure that the source matches the first argument
var found bool = false
for _, src := range sources.([]interface{}) {
sourceMap, ok := src.(map[string]interface{})
if !ok {
log.Fatal("Failed to convert source to map")
}
src, err := CreateSource(sourceMap)
if err != nil {
log.Fatal(err)
}
if src.GetName() == args[0] {
source = src
var source platforms.Source
for _, s := range sourceSlice {
if s.GetName() == args[0] {
source = s
found = true
log.Info("Found source", "source", source.GetName())
break
}
}
if !found {
log.Fatal("Failed to find source in config")
log.Fatal("Source not found", "source", args[0])
}

// Check if OAuth works (remove this later!)
// if blogger, ok := destinationSlice[0].(Blogger); ok {
// token, err := blogger.authorize(viper.GetString("google-client-id"), viper.GetString("google-client-secret"))
// if err != nil {
// log.Fatal(err)
// }
// log.Info("", "token", token)
// blogId, err := blogger.getBlogId(token)
// if err != nil {
// log.Fatal(err)
// }
// log.Info("", "blog id", blogId)
// }

// Pull the data from the source
var options PushPullOptions
var options platforms.PushPullOptions
switch source.GetType() {
case "blogger":
_, accessToken, blogId, err := prepareBlogger(source, nil)
if err != nil {
log.Fatal(err)
}

options = PushPullOptions{
options = platforms.PushPullOptions{
AccessToken: accessToken,
BlogId: blogId,
PostUrl: args[1],
}
case "markdown":
options = PushPullOptions{
options = platforms.PushPullOptions{
Filepath: args[1],
}
}
Expand All @@ -114,14 +75,14 @@ var publishCmd = &cobra.Command{
var found bool = true
switch destination.GetType() {
case "markdown":
options = PushPullOptions{}
options = platforms.PushPullOptions{}

case "blogger":
_, accessToken, blogId, err := prepareBlogger(nil, destination)
if err != nil {
log.Fatal(err)
}
options = PushPullOptions{
options = platforms.PushPullOptions{
AccessToken: accessToken,
BlogId: blogId,
}
Expand Down Expand Up @@ -166,57 +127,57 @@ func init() {
}

// Return the Blogger object and a string with the access token, the blog ID, and an error (if one occurred)
func prepareBlogger(source Source, destination Destination) (Blogger, string, string, error) {
func prepareBlogger(source platforms.Source, destination platforms.Destination) (platforms.Blogger, string, string, error) {
// Check if the user passed a source or destination. Exactly one should be passed.
var platform interface{}
if source == nil && destination == nil {
return Blogger{}, "", "", fmt.Errorf("no source or destination passed")
return platforms.Blogger{}, "", "", fmt.Errorf("no source or destination passed")
} else if source != nil && destination != nil {
return Blogger{}, "", "", fmt.Errorf("both source and destination passed")
return platforms.Blogger{}, "", "", fmt.Errorf("both source and destination passed")
} else if source != nil {
platform = source
} else if destination != nil {
platform = destination
} else {
return Blogger{}, "", "", fmt.Errorf("failed to determine if source or destination was passed")
return platforms.Blogger{}, "", "", fmt.Errorf("failed to determine if source or destination was passed")
}

// Convert source to Blogger
var blogger Blogger
if tmpBlogger, ok := platform.(Blogger); ok {
var blogger platforms.Blogger
if tmpBlogger, ok := platform.(platforms.Blogger); ok {
log.Debug("Asserted that source is Blogger successfully")
blogger = tmpBlogger
} else {
return Blogger{}, "", "", fmt.Errorf("failed to assert that source is Blogger - potentially due to being called on a non-Blogger source")
return platforms.Blogger{}, "", "", fmt.Errorf("failed to assert that source is Blogger - potentially due to being called on a non-Blogger source")
}
// If the refresh token exists in Viper, pass that to Blogger.Authorize. Otherwise, pass an empty string
refreshToken := viper.GetString("google-refresh-token")
var accessToken string
var err error
if refreshToken == "" {
log.Warn("No refresh token found in Viper")
accessToken, refreshToken, err = blogger.authorize(viper.GetString("google-client-id"), viper.GetString("google-client-secret"), "")
accessToken, refreshToken, err = blogger.Authorize(viper.GetString("google-client-id"), viper.GetString("google-client-secret"), "")
if err != nil {
return Blogger{}, "", "", err
return platforms.Blogger{}, "", "", err
}
// Write the refresh token to the config file
log.Info("Writing refresh token to Viper")
viper.Set("google-refresh-token", refreshToken)
err = viper.WriteConfig()
if err != nil {
return Blogger{}, "", "", err
return platforms.Blogger{}, "", "", err
}
} else {
log.Info("Found refresh token in Viper")
accessToken, _, err = blogger.authorize(viper.GetString("google-client-id"), viper.GetString("google-client-secret"), refreshToken)
accessToken, _, err = blogger.Authorize(viper.GetString("google-client-id"), viper.GetString("google-client-secret"), refreshToken)
}
if err != nil {
return Blogger{}, "", "", err
return platforms.Blogger{}, "", "", err
}

blogId, err := blogger.getBlogId(accessToken)
blogId, err := blogger.GetBlogId(accessToken)
if err != nil {
return Blogger{}, "", "", err
return platforms.Blogger{}, "", "", err
}
return blogger, accessToken, blogId, nil
}
48 changes: 15 additions & 33 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,51 @@ module github.com/slashtechno/cross-blogger
go 1.19

require (
github.com/JohannesKaufmann/html-to-markdown v1.4.0
github.com/alexflint/go-arg v1.4.3
github.com/JohannesKaufmann/html-to-markdown v1.6.0
github.com/charmbracelet/log v0.4.0
github.com/go-resty/resty/v2 v2.13.1
github.com/gomarkdown/markdown v0.0.0-20221013030248-663e2500819c
github.com/gosimple/slug v1.14.0
github.com/imdario/mergo v0.3.16
github.com/sirupsen/logrus v1.9.2
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/spf13/afero v1.11.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/subosito/gotenv v1.6.0
github.com/tidwall/gjson v1.16.0
github.com/yuin/goldmark v1.7.2
go.abhg.dev/goldmark/frontmatter v0.2.0
golang.org/x/oauth2 v0.18.0
golang.org/x/oauth2 v0.21.0
gopkg.in/yaml.v2 v2.4.0
)

require (
cloud.google.com/go/compute v1.24.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/PuerkitoBio/goquery v1.8.1 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
cloud.google.com/go/compute/metadata v0.3.0 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/PuerkitoBio/goquery v1.9.2 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.10.0 // indirect
github.com/charmbracelet/lipgloss v0.11.0 // indirect
github.com/charmbracelet/x/ansi v0.1.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.33.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/alexflint/go-scalar v1.1.0 // indirect
github.com/joho/godotenv v1.5.1
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)
Loading

0 comments on commit e5e1d5d

Please sign in to comment.