Skip to content

Commit

Permalink
chore: go reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
robgordon89 committed May 29, 2023
1 parent ce62207 commit 6dfad7f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# GitHub Action to Purge Cloudflare Cache 🗑️


## Usage

All sensitive variables should be [set as encrypted secrets](https://help.github.com/en/articles/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables) in the action's configuration.
Expand Down
45 changes: 25 additions & 20 deletions action.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
// "context"
"bytes"
"context"
"encoding/json"
Expand All @@ -26,54 +25,60 @@ type CFPurgeWithPrefixes struct {
Prefixes []string `json:"prefixes,omitempty"`
}

func ErrCheck(e error) {
if e != nil {
log.Println(e)
}
}

func main() {
api, err := cloudflare.NewWithAPIToken(CFToken)
ErrCheck(err)
if err != nil {
log.Println(err)
}

ctx := context.Background()

zoneName, err := api.ZoneIDByName(CFZoneName)
ErrCheck(err)
if err != nil {
log.Println(err)
}

// purgeHosts, present := os.LookupEnv("INPUT_CF_PURGE_HOSTS")
// purgeURLs, presentURL := os.LookupEnv("INPUT_CF_PURGE_URLS")
// _ = purgeURLs //omit 'declared but not used' err
// _ = purgeHosts
if len(purgeHosts) > 1 {
pcr := cloudflare.PurgeCacheRequest{Hosts: strings.Split(purgeHosts, ",")}
purge, err := api.PurgeCache(ctx, zoneName, pcr)
ErrCheck(err)
if err != nil {
log.Println(err)
}
fmt.Printf("Success: %t", purge.Response.Success)
} else if len(purgeURLs) > 1 {
pcr := cloudflare.PurgeCacheRequest{Files: strings.Split(purgeURLs, ",")}
purge, err := api.PurgeCache(ctx, zoneName, pcr)
ErrCheck(err)
if err != nil {
log.Println(err)
}
fmt.Printf("Success: %t", purge.Response.Success)
} else if len(purgePrefixes) > 1 {
client := &http.Client{}
purgePrefData := CFPurgeWithPrefixes{
Prefixes: strings.Split(purgePrefixes, ","),
}
purgeJSON, errj := json.Marshal(purgePrefData)
ErrCheck(errj)
purgeJSON, err := json.Marshal(purgePrefData)
if err != nil {
log.Println(err)
}
purgeReq, err := http.NewRequest("POST", "https://api.cloudflare.com/client/v4/zones/"+zoneName+"/purge_cache",
bytes.NewBuffer(purgeJSON))
ErrCheck(err)
if err != nil {
log.Println(err)
}
purgeReq.Header.Set("Authorization", "Bearer "+CFToken)
purgeReq.Header.Set("Content-Type", "application/json")
purgeResp, err := client.Do(purgeReq)
ErrCheck(err)
if err != nil {
log.Println(err)
}
defer purgeResp.Body.Close()
fmt.Printf("Success status code: %s", purgeResp.Status)
} else {
purge, err := api.PurgeEverything(ctx, zoneName)
ErrCheck(err)
if err != nil {
log.Println(err)
}
fmt.Printf("Success: %t", purge.Response.Success)
}
}
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: "Cloudflare Purge Cache GitHub Action"
description: "Purge a zone's cache via the Cloudflare API"
author: aorfanos
inputs:
cf_token:
cf_token:
description: "A valid CF API token with Zones.Get and Zones.Purge permissions"
required: true
cf_zone_name:
cf_zone_name:
description: "CF zone name manageable by ${cf_token}"
required: false
cf_purge_hosts:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module remotecompany.com/purge-cf-cache-action
module github.com/mailergroup/purge-cf-cache-action

go 1.14
go 1.16

require github.com/cloudflare/cloudflare-go v0.17.0
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/cloudflare/cloudflare-go v0.17.0 h1:lbNR+leC9ZHZteksrTwYVlxg+4eMx5f8w
github.com/cloudflare/cloudflare-go v0.17.0/go.mod h1:sPWL/lIC6biLEdyGZwBQ1rGQKF1FhM7N60fuNiFdYTI=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
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/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -11,10 +12,12 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.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/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
Expand All @@ -30,4 +33,5 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 6dfad7f

Please sign in to comment.