Skip to content

Commit

Permalink
add access_token support
Browse files Browse the repository at this point in the history
  • Loading branch information
wiquanAppD authored and justmiles committed Oct 28, 2022
1 parent c4e4b43 commit c2742b0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ dist
.vscode
*.code-workspace
.envrc
.idea/
***/*.secret
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ docker run -v $PWD:/src -w /src goreleaser/goreleaser --snapshot --skip-publish

For best practice we recommend you [authenticate using an API token](https://id.atlassian.com/manage/api-tokens).

However, you may also use [Personal Access Tokens](https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html),
which may help if your company uses SSO.

- CONFLUENCE_USERNAME - username for Confluence Cloud. When using API tokens set this to your full email.
- CONFLUENCE_PASSWORD - API token or password for Confluence Cloud
- CONFLUENCE_ENDPOINT - endpoint for Confluence Cloud, eg `https://mycompanyname.atlassian.net/wiki`

- CONFLUENCE_ACCESS_TOKEN - Bearer access token to use (instead of API token)
-
## Usage

```txt
Expand All @@ -57,20 +61,21 @@ Usage:
markdown2confluence [flags]
Flags:
-d, --debug Enable debug logging
-e, --endpoint string Confluence endpoint. (Alternatively set CONFLUENCE_ENDPOINT environment variable) (default "https://mydomain.atlassian.net/wiki")
-x, --exclude strings list of exclude file patterns (regex) that will be applied on markdown file paths
-w, --hardwraps Render newlines as <br />
-h, --help help for markdown2confluence
-m, --modified-since int Only upload files that have modifed in the past n minutes
--parent string Optional parent page to next content under
-p, --password string Confluence password. (Alternatively set CONFLUENCE_PASSWORD environment variable)
-s, --space string Space in which page should be created
-c, --comment string Add a comment to the page (optional)
-t, --title string Set the page title on upload (defaults to filename without extension)
--use-document-title Will use the Markdown document title (# Title) if available
-u, --username string Confluence username. (Alternatively set CONFLUENCE_USERNAME environment variable)
--version version for markdown2confluence
-a, --access-token string Confluence access-token. (Alternatively set CONFLUENCE_ACCESS_TOKEN environment variable)
-c, --comment string (Optional) Add comment to page
-d, --debug Enable debug logging
-e, --endpoint string Confluence endpoint. (Alternatively set CONFLUENCE_ENDPOINT environment variable) (default "https://mydomain.atlassian.net/wiki")
-x, --exclude strings list of exclude file patterns (regex) for that will be applied on markdown file paths
-w, --hardwraps Render newlines as <br />
-h, --help help for markdown2confluence
-m, --modified-since int Only upload files that have modifed in the past n minutes
--parent string Optional parent page to next content under
-p, --password string Confluence password. (Alternatively set CONFLUENCE_PASSWORD environment variable)
-s, --space string Space in which page should be created
-t, --title string Set the page title on upload (defaults to filename without extension)
--use-document-title Will use the Markdown document title (# Title) if available
-u, --username string Confluence username. (Alternatively set CONFLUENCE_USERNAME environment variable)
--version version for markdown2confluence
```

## Examples
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&m.Comment, "comment", "c", "", "(Optional) Add comment to page")
rootCmd.PersistentFlags().StringVarP(&m.Username, "username", "u", "", "Confluence username. (Alternatively set CONFLUENCE_USERNAME environment variable)")
rootCmd.PersistentFlags().StringVarP(&m.Password, "password", "p", "", "Confluence password. (Alternatively set CONFLUENCE_PASSWORD environment variable)")
rootCmd.PersistentFlags().StringVarP(&m.AccessToken, "access-token", "a", "", "Confluence access-token. (Alternatively set CONFLUENCE_ACCESS_TOKEN environment variable)")
rootCmd.PersistentFlags().StringVarP(&m.Endpoint, "endpoint", "e", lib.DefaultEndpoint, "Confluence endpoint. (Alternatively set CONFLUENCE_ENDPOINT environment variable)")
rootCmd.PersistentFlags().StringVar(&m.Parent, "parent", "", "Optional parent page to next content under")
rootCmd.PersistentFlags().BoolVarP(&m.Debug, "debug", "d", false, "Enable debug logging")
Expand Down
4 changes: 4 additions & 0 deletions confluence.secret.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export CONFLUENCE_USERNAME=jwick@blacksuit.com
export CONFLUENCE_PASSWORD=bullets
export CONFLUENCE_ENDPOINT=https://mycompanyname.atlassian.net/wiki
export CONFLUENCE_PERSONAL_ACCESS_TOKEN=DEADBEEF
14 changes: 12 additions & 2 deletions lib/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Markdown2Confluence struct {
Since int
Username string
Password string
AccessToken string
Endpoint string
Parent string
SourceMarkdown []string
Expand All @@ -54,6 +55,7 @@ func (m *Markdown2Confluence) CreateClient() {
m.client = new(confluence.Client)
m.client.Username = m.Username
m.client.Password = m.Password
m.client.AccessToken = m.AccessToken
m.client.Endpoint = m.Endpoint
m.client.Debug = m.Debug
}
Expand All @@ -74,6 +76,11 @@ func (m *Markdown2Confluence) SourceEnvironmentVariables() {
m.Password = s
}

s = os.Getenv("CONFLUENCE_ACCESS_TOKEN")
if s != "" {
m.AccessToken = s
}

s = os.Getenv("CONFLUENCE_ENDPOINT")
if s != "" {
m.Endpoint = s
Expand All @@ -85,10 +92,10 @@ func (m Markdown2Confluence) Validate() error {
if m.Space == "" {
return fmt.Errorf("--space is not defined")
}
if m.Username == "" {
if m.Username == "" && m.AccessToken == "" {
return fmt.Errorf("--username is not defined")
}
if m.Password == "" {
if m.Password == "" && m.AccessToken == "" {
return fmt.Errorf("--password is not defined")
}
if m.Endpoint == "" {
Expand All @@ -103,6 +110,9 @@ func (m Markdown2Confluence) Validate() error {
if len(m.SourceMarkdown) > 1 && m.Title != "" {
return fmt.Errorf("You can not set the title for multiple files")
}
if m.AccessToken == "" && m.Username == "" {
return fmt.Errorf("--access-token is not defined")
}
return nil
}

Expand Down

0 comments on commit c2742b0

Please sign in to comment.