Last active
November 18, 2022 06:42
-
-
Save AfvanMoopen/d5c32a88e833cb2f0ed6b09c8d1a10c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"golang.org/x/oauth2" | |
"golang.org/x/oauth2/google" | |
"google.golang.org/api/option" | |
"google.golang.org/api/sheets/v4" | |
"gopkg.in/yaml.v2" | |
) | |
type Configs struct { | |
Config []Config | |
} | |
type Config struct { | |
Groups []string | |
Datasets []string | |
} | |
var cnt int | |
func tokenFromFile(file string) (*oauth2.Token, error) { | |
f, err := os.Open(file) | |
if err != nil { | |
return nil, err | |
} | |
defer f.Close() | |
tok := &oauth2.Token{} | |
err = json.NewDecoder(f).Decode(tok) | |
return tok, err | |
} | |
func saveToken(path string, token *oauth2.Token) { | |
fmt.Printf("Saving credential file to: %s\n", path) | |
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) | |
if err != nil { | |
log.Fatalf("Unable to cache oauth token: %v", err) | |
} | |
defer f.Close() | |
json.NewEncoder(f).Encode(token) | |
} | |
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token { | |
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline) | |
fmt.Printf("Go to the following link in your browser then type the "+ | |
"authorization code: \n%v\n", authURL) | |
var authCode string | |
if _, err := fmt.Scan(&authCode); err != nil { | |
log.Fatalf("Unable to read authorization code: %v", err) | |
} | |
tok, err := config.Exchange(context.TODO(), authCode) | |
if err != nil { | |
log.Fatalf("Unable to retrieve token from web: %v", err) | |
} | |
return tok | |
} | |
func getClient(config *oauth2.Config) *http.Client { | |
tokFile := "token.json" | |
tok, err := tokenFromFile(tokFile) | |
if err != nil { | |
tok = getTokenFromWeb(config) | |
saveToken(tokFile, tok) | |
} | |
return config.Client(context.Background(), tok) | |
} | |
func main() { | |
ctx := context.Background() | |
b, err := os.ReadFile("secret.json") | |
if err != nil { | |
log.Fatalf("Unable to read client secret file") | |
} | |
config, err := google.ConfigFromJSON(b, "https://www.googleapis.com/auth/spreadsheets.readonly") | |
if err != nil { | |
log.Fatalf("Unable to parse the file") | |
} | |
client := getClient(config) | |
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client)) | |
fmt.Print(srv) | |
if err != nil { | |
log.Fatalf("Unable to retrieve sheets clients : %v", err) | |
} | |
files, err := ioutil.ReadDir("bq-accesscontrol-master") | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, f := range files { | |
if f.Name() == "bq-groups" { | |
dir, _ := os.Getwd() | |
files, _ := ioutil.ReadDir(dir + "/bq-accesscontrol-master/bq-groups") | |
path := dir + "/bq-accesscontrol-master/bq-groups" | |
for _, f := range files { | |
file, err := ioutil.ReadFile(path + "/" + f.Name()) | |
if err != nil { | |
log.Fatal(err) | |
} | |
data := make(map[string]interface{}) | |
err_again := yaml.Unmarshal(file, &data) | |
if err_again != nil { | |
log.Fatal(err_again) | |
} | |
datasets := make(map[interface{}]interface{}) | |
groups := make(map[interface{}]interface{}) | |
for k, v := range data { | |
defer func() { | |
if r := recover(); r != nil { | |
fmt.Println("panic occured:", r) | |
} | |
}() | |
if data[k] == nil { | |
continue | |
} | |
// for _, e := range val.(interface{}) { | |
// v := val.MapIndex(e) | |
// fmt.Print(v) | |
// } | |
for k1, v1 := range v.(map[interface{}]interface{}) { | |
if k == "datasets" { | |
datasets[k1] = v1 | |
continue | |
} | |
groups[k1] = v1 | |
} | |
} | |
fmt.Print(f.Name()) | |
var jump []string | |
fmt.Printf("\n") | |
fmt.Printf("Datasets from No : %d\n", cnt) | |
for key, value := range datasets { | |
fmt.Printf("%s -> %s", key, value) | |
jump = append(jump, key.(string)) | |
fmt.Print(jump) | |
} | |
fmt.Println() | |
fmt.Printf("Groups from No : %d\n", cnt) | |
for key, value := range groups { | |
fmt.Printf("%s -> %s", key, value) | |
jump = append(jump, key.(string)) | |
} | |
cnt++ | |
//time.Sleep(3 * time.Second) | |
// fmt.Print(cnt) | |
fmt.Printf("\n\n\n\n") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment