diff --git a/Dockerfile b/Dockerfile index 13f7ed8..b4e9264 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.13-alpine as build +FROM golang:1.14-alpine as build ADD . /app WORKDIR /app RUN go build diff --git a/config.go b/config.go index 646ef30..48dd5bf 100644 --- a/config.go +++ b/config.go @@ -11,12 +11,10 @@ var ( BlogUrl string MediaEndpointUrl string IgnoredWebmentionUrls []string - ActivityStreams bool SyndicationTargets []SyndicationTarget SelectedStorage Storage SelectedMediaStorage MediaStorage SelectedCdn Cdn - SelectedSocials Socials SelectedNotificationServices NotificationServices SelectedImageCompression ImageCompression ) @@ -30,20 +28,20 @@ type config struct { BlogUrl string `env:"BLOG_URL,required"` BaseUrl string `env:"BASE_URL,required"` MediaUrl string `env:"MEDIA_URL"` - GiteaEndpoint string `env:"GITEA_ENDPOINT"` - GiteaToken string `env:"GITEA_TOKEN"` + GitFilepath string `env:"GIT_FILEPATH" envDefault:"/tmp/micropubrepo"` + GitUrl string `env:"GIT_URL"` + GitUsername string `env:"GIT_USERNAME"` + GitPassword string `env:"GIT_PASSWORD"` + GitAuthorName string `env:"GIT_AUTHOR_NAME" envDefault:"hugo-micropub"` + GitAuthorEmail string `env:"GIT_AUTHOR_EMAIL" envDefault:"hugo-micropub@example.com"` BunnyCdnKey string `env:"BUNNY_CDN_KEY"` BunnyCdnStorageKey string `env:"BUNNY_CDN_STORAGE_KEY"` BunnyCdnStorageName string `env:"BUNNY_CDN_STORAGE_NAME"` - MicroblogUrl string `env:"MICROBLOG_URL"` - MicroblogToken string `env:"MICROBLOG_TOKEN"` TelegramUserId int64 `env:"TELEGRAM_USER_ID"` TelegramBotToken string `env:"TELEGRAM_BOT_TOKEN"` IgnoredWebmentionUrls []string `env:"WEBMENTION_IGNORED" envSeparator:","` SyndicationTargets []string `env:"SYNDICATION" envSeparator:","` TinifyKey string `env:"TINIFY_KEY"` - ActivityStreams bool `env:"ACTIVITY_STREAMS" envDefault:"false"` - JsonpubUrl string `env:"JSONPUB_URL"` } func initConfig() (err error) { @@ -63,8 +61,6 @@ func initConfig() (err error) { MediaEndpointUrl = cfg.BaseUrl + "media" // Ignored Webmention URLs (optional) IgnoredWebmentionUrls = cfg.IgnoredWebmentionUrls - // Has ActivityStreams support (optional) - ActivityStreams = cfg.ActivityStreams // Syndication Targets (optional) targets := make([]SyndicationTarget, 0) for _, url := range cfg.SyndicationTargets { @@ -76,11 +72,15 @@ func initConfig() (err error) { SyndicationTargets = targets // Find selected storage SelectedStorage = func() Storage { - // Gitea - if len(cfg.GiteaEndpoint) > 0 && len(cfg.GiteaToken) >= 0 { - return &Gitea{ - endpoint: cfg.GiteaEndpoint, - token: cfg.GiteaToken, + // Git + if len(cfg.GitFilepath) > 0 && len(cfg.GitUrl) > 0 && len(cfg.GitUsername) > 0 && len(cfg.GitPassword) > 0 && len(cfg.GitAuthorName) > 0 && len(cfg.GitAuthorEmail) > 0 { + return &Git{ + filepath: cfg.GitFilepath, + url: cfg.GitUrl, + username: cfg.GitUsername, + password: cfg.GitPassword, + name: cfg.GitAuthorName, + email: cfg.GitAuthorEmail, } } return nil @@ -115,25 +115,6 @@ func initConfig() (err error) { if SelectedCdn == nil { log.Println("no CDN configured") } - // Find configured social networks (optional) - SelectedSocials = func() Socials { - var socials []Social = nil - // Microblog.pub - if len(cfg.MicroblogUrl) > 0 && len(cfg.MicroblogToken) > 0 { - socials = append(socials, &MicroblogPub{ - url: cfg.MicroblogUrl, - token: cfg.MicroblogToken, - }) - } - // Jsonpub - if len(cfg.JsonpubUrl) > 0 { - socials = append(socials, &Jsonpub{url: cfg.JsonpubUrl}) - } - return socials - }() - if SelectedSocials == nil { - log.Println("no social networks configured") - } // Find configured notification services (optional) SelectedNotificationServices = func() NotificationServices { var notificationServices []NotificationService = nil diff --git a/entry.go b/entry.go index 3a60e66..123f063 100644 --- a/entry.go +++ b/entry.go @@ -310,7 +310,7 @@ func ReadEntry(url string) (entry *Entry, err error) { if err != nil { return } - fileContent, _, exists, err := SelectedStorage.ReadFile(filePath) + fileContent, exists, err := SelectedStorage.ReadFile(filePath) if err != nil || !exists { err = errors.New("failed to read file or entry doesn't exist") return diff --git a/go.mod b/go.mod index 969c8d9..9e01e09 100644 --- a/go.mod +++ b/go.mod @@ -1,20 +1,17 @@ module codeberg.org/jlelse/hugo-micropub -go 1.13 +go 1.14 require ( codeberg.org/jlelse/tinify v0.0.0-20200123222407-7fc9c21822b0 github.com/andybalholm/cascadia v1.1.0 // indirect - github.com/caarlos0/env/v6 v6.1.0 - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/caarlos0/env/v6 v6.2.1 + github.com/go-git/go-git/v5 v5.0.0 github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20190904012038-b33efeebc785+incompatible - github.com/google/go-cmp v0.4.0 // indirect - github.com/kr/pretty v0.2.0 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/stretchr/testify v1.5.0 // indirect github.com/technoweenie/multipartstreamer v1.0.1 // indirect - golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect - gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect + golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6 // indirect + golang.org/x/net v0.0.0-20200319234117-63522dbf7eec // indirect + golang.org/x/sys v0.0.0-20200317113312-5766fd39f98d // indirect gopkg.in/yaml.v2 v2.2.8 willnorris.com/go/webmention v0.0.0-20200126231626-5a55fff6bf71 ) diff --git a/go.sum b/go.sum index 497d098..98e427e 100644 --- a/go.sum +++ b/go.sum @@ -1,47 +1,101 @@ codeberg.org/jlelse/tinify v0.0.0-20200123222407-7fc9c21822b0 h1:pJX79kTd01NtxEnzhfd4OU2SY9fquKVoO47DUeNKe+8= codeberg.org/jlelse/tinify v0.0.0-20200123222407-7fc9c21822b0/go.mod h1:X6cM4Sn0aL/4VQ/ku11yxmiV0WIk5XAaYEPHQLQjFFM= +github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs= +github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= +github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o= github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo= github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= -github.com/caarlos0/env/v6 v6.1.0 h1:4FbM+HmZA/Q5wdSrH2kj0KQXm7xnhuO8y3TuOTnOvqc= -github.com/caarlos0/env/v6 v6.1.0/go.mod h1:iUA6X3VCAOwDhoqvgKlTGjjwJzQseIJaFYApUqQkt+8= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= +github.com/caarlos0/env/v6 v6.2.1 h1:/bFpX1dg4TNioJjg7mrQaSrBoQvRfLUHNfXivdFbbEo= +github.com/caarlos0/env/v6 v6.2.1/go.mod h1:3LpmfcAYCG6gCiSgDLaFR5Km1FRpPwFvBbRcjHar6Sw= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= +github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= +github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0= +github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= +github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= +github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E= +github.com/go-git/go-billy/v5 v5.0.0 h1:7NQHvd9FVid8VL4qVUMm8XifBK+2xCoZ2lSk0agRrHM= +github.com/go-git/go-billy/v5 v5.0.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= +github.com/go-git/go-git-fixtures/v4 v4.0.1 h1:q+IFMfLx200Q3scvt2hN79JsEzy4AmBTp/pqnefH+Bc= +github.com/go-git/go-git-fixtures/v4 v4.0.1/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw= +github.com/go-git/go-git/v5 v5.0.0 h1:k5RWPm4iJwYtfWoxIJy4wJX9ON7ihPeZZYC1fLYDnpg= +github.com/go-git/go-git/v5 v5.0.0/go.mod h1:oYD8y9kWsGINPFJoLdaScGCN6dlKg23blmClfZwtUVA= github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20190904012038-b33efeebc785+incompatible h1:OT02onvXX618RBcjxeUA4H7d1PSm5Apg4IET72VgVlE= github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20190904012038-b33efeebc785+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM= +github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= -github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY= +github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.0 h1:DMOzIV76tmoDNE9pX6RSN0aDtCYeCg5VueieJaAo1uw= -github.com/stretchr/testify v1.5.0/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0/go.mod h1:IXCdmsXIht47RaVFLEdVnh1t+pgYtTAhQGj73kz+2DM= +github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= +github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= +golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6 h1:TjszyFsQsyZNHwdVdZ5m7bjmreu0znc2kRYsEml9/Ww= +golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= -golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0= +golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200319234117-63522dbf7eec h1:w0SItUiQ4sBiXBAwWNkyu8Fu2Qpn/dtDIcoPkPDqjRw= +golang.org/x/net v0.0.0-20200319234117-63522dbf7eec/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So= +golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200317113312-5766fd39f98d h1:62ap6LNOjDU6uGmKXHJbSfciMoV+FeI1sRXx/pLDL44= +golang.org/x/sys v0.0.0-20200317113312-5766fd39f98d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= +gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= willnorris.com/go/webmention v0.0.0-20200126231626-5a55fff6bf71 h1:F//bgirx4BIsJYHrqAYCZHODn0gSRej/KfueFlarXhs= diff --git a/micropub.go b/micropub.go index d762823..71dbeaf 100644 --- a/micropub.go +++ b/micropub.go @@ -94,11 +94,6 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) { } // Send webmentions go SendWebmentions(location) - go func() { - if SelectedSocials != nil { - SelectedSocials.Post(location, entry.title) - } - }() }() return } diff --git a/social.go b/social.go deleted file mode 100644 index ae0fc87..0000000 --- a/social.go +++ /dev/null @@ -1,78 +0,0 @@ -package main - -import ( - "bytes" - "encoding/json" - "net/http" -) - -type Social interface { - Post(location string, text string) -} - -type Socials []Social - -// Post to all socials -func (socials *Socials) Post(location string, text string) { - for _, social := range *socials { - social.Post(location, text) - } -} - -// Microblog.pub -type MicroblogPub struct { - url string - token string -} - -func (social *MicroblogPub) Post(location string, text string) { - if len(location) == 0 { - // Doesn't make sense to have empty URL - return - } - client := &http.Client{} - var req *http.Request - if ActivityStreams { - // Just boost the Activity - note := &struct { - Id string `json:"id"` - }{ - Id: location, - } - bytesRepresentation, err := json.Marshal(note) - if err != nil { - return - } - req, _ = http.NewRequest("POST", social.url+"api/boost", bytes.NewBuffer(bytesRepresentation)) - } else { - // Post the Activity - if len(text) == 0 { - text = location - } - note := &struct { - Content string `json:"content"` - }{ - Content: "[" + text + "](" + location + ")", - } - bytesRepresentation, err := json.Marshal(note) - if err != nil { - return - } - req, _ = http.NewRequest("POST", social.url+"api/new_note", bytes.NewBuffer(bytesRepresentation)) - } - if req == nil { - return - } - req.Header.Add("Content-Type", "application/json") - req.Header.Add("Authorization", "Bearer "+social.token) - _, _ = client.Do(req) -} - -// Jsonpub -type Jsonpub struct { - url string -} - -func (social *Jsonpub) Post(_ string, _ string) { - _, _ = http.Post(social.url, "", nil) -} \ No newline at end of file diff --git a/storage.go b/storage.go index 8590e88..7139050 100644 --- a/storage.go +++ b/storage.go @@ -1,134 +1,154 @@ package main import ( - "bytes" - "encoding/base64" - "encoding/json" "errors" + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing/object" + gitHttp "github.com/go-git/go-git/v5/plumbing/transport/http" "io/ioutil" - "net/http" - "net/url" - "strings" + "os" + "path" + "time" ) type Storage interface { CreateFile(path string, file string, message string) (err error) UpdateFile(path string, file string, message string) (err error) - ReadFile(path string) (content string, sha string, exists bool, err error) + ReadFile(path string) (content string, exists bool, err error) } -// Gitea -type Gitea struct { - endpoint string - token string +type Git struct { + filepath string + url string + username string + password string + name string + email string } -type giteaCommitRequest struct { - Content string `json:"content"` - Message string `json:"message"` - SHA string `json:"sha,omitempty"` -} - -type giteaReadResponse struct { - Type string `json:"type"` - Content string `json:"content"` - SHA string `json:"sha"` -} - -type giteaErrorResponse struct { - Message string `json:"message"` -} - -func (gitea *Gitea) CreateFile(path string, file string, message string) (err error) { - request := &giteaCommitRequest{ - Content: base64.StdEncoding.EncodeToString([]byte(file)), - Message: message, - } - bytesRepresentation, err := json.Marshal(request) - if err != nil { - return errors.New("failed to marshal json before committing") - } - resp, err := http.Post(gitea.endpoint+url.QueryEscape(path)+"?access_token="+gitea.token, "application/json", bytes.NewBuffer(bytesRepresentation)) - if err != nil || resp.StatusCode != 201 { - return errors.New("failed to create file in repo") - } - return nil -} - -func (gitea *Gitea) UpdateFile(path string, file string, message string) (err error) { - _, sha, exists, err := gitea.ReadFile(path) - if err != nil { - return err - } - if !exists { - // File doesn't exist, create it - return gitea.CreateFile(path, file, message) - } - request := &giteaCommitRequest{ - Content: base64.StdEncoding.EncodeToString([]byte(file)), - Message: message, - SHA: sha, - } - bytesRepresentation, err := json.Marshal(request) - if err != nil { - return errors.New("failed to marshal json before committing") - } - req, err := http.NewRequest(http.MethodPut, gitea.endpoint+url.QueryEscape(path)+"?access_token="+gitea.token, bytes.NewBuffer(bytesRepresentation)) - if err != nil { - return errors.New("error making update request") - } - req.Header.Set("Content-type", "application/json") - client := &http.Client{} - resp, err := client.Do(req) - if err != nil || resp.StatusCode != 200 { - return errors.New("failed to update file in repo") - } - return nil -} - -func (gitea *Gitea) ReadFile(path string) (content string, sha string, exists bool, err error) { - exists = false - resp, err := http.Get(gitea.endpoint + url.QueryEscape(path) + "?access_token=" + gitea.token) - if err != nil || resp.StatusCode != 200 { - if resp != nil { - defer resp.Body.Close() - body, readErr := ioutil.ReadAll(resp.Body) - if readErr != nil { - err = errors.New("failed reading Gitea error response") - return - } - errorResponse := &giteaErrorResponse{} - marshalErr := json.Unmarshal(body, &errorResponse) - if marshalErr != nil { - err = errors.New("failed parsing Gitea error response") - return - } - exists = !strings.Contains(errorResponse.Message, "does not exist") - if !exists { - return +func (g Git) init() (r *git.Repository, w *git.Worktree, err error) { + // Open repo + r, err = git.PlainOpen(g.filepath) + if err == nil { + // Try to get work tree + w, err = r.Worktree() + if err == nil { + // Try to pull + err = w.Pull(&git.PullOptions{ + Auth: &gitHttp.BasicAuth{ + Username: g.username, + Password: g.password, + }, + SingleBranch: true, + }) + if err == git.NoErrAlreadyUpToDate { + err = nil } } - err = errors.New("failed to read file in repo") - return } - exists = true - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) if err != nil { - err = errors.New("failed reading file in repo") - return + // Delete old things + _ = os.RemoveAll(g.filepath) + // Clone + r, err = git.PlainClone(g.filepath, false, &git.CloneOptions{ + Auth: &gitHttp.BasicAuth{ + Username: g.username, + Password: g.password, + }, + URL: g.url, + Depth: 1, + SingleBranch: true, + }) + if err != nil { + err = errors.New("failed to clone repo") + } + if err == nil { + w, err = r.Worktree() + if err != nil { + err = errors.New("failed to get work tree") + } + } } - readResponse := &giteaReadResponse{} - err = json.Unmarshal(body, &readResponse) - if err != nil { - err = errors.New("failed parsing Gitea response") - return - } - decodedContentBytes, err := base64.StdEncoding.DecodeString(readResponse.Content) - if err != nil { - err = errors.New("failed decoding file content") - } - content = string(decodedContentBytes) - sha = readResponse.SHA return +} + +func (g Git) push(repository *git.Repository) error { + err := repository.Push(&git.PushOptions{ + Auth: &gitHttp.BasicAuth{ + Username: g.username, + Password: g.password, + }, + }) + if err == nil || err == git.NoErrAlreadyUpToDate { + return nil + } else { + return errors.New("failed to push to remote") + } +} + +func (g Git) CreateFile(filepath string, file string, message string) (err error) { + _, _, err = g.init() + if err != nil { + err = errors.New("failed to initialize repo") + return + } + joinedPath := path.Join(g.filepath, filepath) + if _, e := os.Stat(joinedPath); e == nil { + return errors.New("file already exists") + } else { + return g.UpdateFile(filepath, file, message) + } +} + +func (g Git) UpdateFile(filepath string, file string, message string) error { + repo, w, err := g.init() + if err != nil { + return errors.New("failed to initialize repo") + } + joinedPath := path.Join(g.filepath, filepath) + _ = os.MkdirAll(path.Dir(joinedPath), 0755) + err = ioutil.WriteFile(joinedPath, []byte(file), 0644) + if err != nil { + return errors.New("failed to write to file") + } + _, err = w.Add(filepath) + if err != nil { + return errors.New("failed to stage file") + } + if len(message) == 0 { + message = "Add " + filepath + } + _, err = w.Commit(message, &git.CommitOptions{ + Author: &object.Signature{ + Name: g.name, + Email: g.email, + When: time.Now(), + }, + }) + if err != nil { + return errors.New("failed to commit file") + } + return g.push(repo) +} + +func (g Git) ReadFile(filepath string) (content string, exists bool, err error) { + _, _, err = g.init() + if err != nil { + err = errors.New("failed to initialize repo") + return + } + joinedPath := path.Join(g.filepath, filepath) + if _, e := os.Stat(joinedPath); e == nil { + exists = true + if b, e := ioutil.ReadFile(joinedPath); e == nil { + content = string(b) + return + } else { + err = errors.New("failed to read file") + return + } + } else { + exists = false + return + } } \ No newline at end of file