From 6c03f880da80aefbfd6df7a1df9c622aa38e8b51 Mon Sep 17 00:00:00 2001 From: Kevin Offet Date: Thu, 13 Apr 2023 15:13:38 -0400 Subject: [PATCH] *gitea remote and push --- examples/example_scaffold-go.toml | 106 ++++++++++++++++++++++++++++++ main.go | 22 ++++++- 2 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 examples/example_scaffold-go.toml diff --git a/examples/example_scaffold-go.toml b/examples/example_scaffold-go.toml new file mode 100644 index 0000000..3f7a669 --- /dev/null +++ b/examples/example_scaffold-go.toml @@ -0,0 +1,106 @@ +# New Go Project configuration file + +# Format: TOML - see https://toml.io for details + +# Go module path is of the form basePath/projectName +# must be addressable if you are going to publish +module_basepath = "somewhere.org" + +# where to put new project directories +# within the user's home directory +projects_basedir = "projects/goapps" + +# go example dirs +# /cmd, /pkg, /web, /api, /init, /vendor, /internal, /bin, /build, +# /cfg, /configs, /docs, /scripts, /tools, /examples, /tests, +# /deployments, /deploy +# or whatever you like + +# Must be string, will be parsed to correct type +# Remember perms are in octal +project_dir_permissions = "0700" +file_permissions = "0660" + +########################################### +# Commands to run +########################################### +# after the newProjectDir is created and +# after the sample dir contents are copied over +### before git is initialized (local and remote) +# For the command: go mod init full_module_name -- use the your module_basename +# scaffold will add "/newProjectName" to it automatically +# so ["go", "mod", "init", "modulebase.net"] becomes ==> ["go", "mod", "init", "modulebase.net/NewPojectName"] +commands = [["go", "mod", "init", "somewhere.org"], ["go", "mod", "tidy"]] + +########################################## + +# Git +setup_git = true + +# Do you have a remote location? +# Do you have autologins setup correctly with ssh keys? +# Using your .ssh/config file might be helpful + +# setup remote repo? +create_remote_repo = true + +# remote repo user@location +remote_user = "auser@someserver.adomain.org" + +# remote repo base dir +# relative to remote user home dir +# for auser@someserver.adomain.org:/home/auser/git -- lists git dir as absolute path +# for auser@someserver.adomain.org:git -- lists git dir as relative path + +############################ +# remote location MUST already exist on remote server +############################ +remote_location = "gitdirectory/goprojects" + +# default remote short name +remote_label = "origin" + +# Gitea -- do you use it somewhere + +############################ +# Does the Gitea app.ini have +# +# [repository] +# ENABLE_PUSH_CREATE_USER = true +# ENABLE_PUSH_CREATE_ORG = true +# +# it's needed for scaffold to push to gitea +############################ + +############################ +# +# 4 pieces of info are needed +# 1) the server domain name eg gitea.adomain.org +# 2) the ssh port if it's not the default 22 +# 3) the server account that clients connect to for ssh +# sometimes it's git or gitea or gitadmin or +# 4) the gitea user account username +# (if your gitea account is mrspecial then it's mrspecial) +# +# items 1, 2 and 3 should be in a Host section in your .ssh/config file +# +# In a section like +# Host gitea +# Hostname gitea.adomain.org +# User serveraccount +# Port 22222 +# IdentitiesOnly yes +# PreferredAuthentications publickey +# IdentityFile ~/.ssh/your_id_file_private +# +# Then scaffold will set a git remote for gitea with: +# +# git remote add :/.git +# +############################ + +setup_gitea = true +gitea_server_account = "gitea-account" +gitea_ssh_host_label = "gitea" +gitea_user_account = "auser" +remote_label_gitea = "gitea" diff --git a/main.go b/main.go index 5184ad9..a8e4f1f 100644 --- a/main.go +++ b/main.go @@ -30,8 +30,13 @@ type ( Create_Remote_Repo bool Remote_User string Remote_Location string - Remote_Shortname string + Remote_Label string Commands [][]string + Setup_gitea bool + Gitea_server_account string // should be in ~/.ssh/config + Gitea_ssh_host_label string + Gitea_user_account string + Remote_Label_gitea string } ScaffoldSetup struct { TestKey string @@ -235,7 +240,7 @@ func main() { // add remote to local repo new_remote_repo := npsetup.Remote_User + ":" + npsetup.Remote_Location + "/" + newProjectName + ".git" - _, err = exec.Command("git", "remote", "add", npsetup.Remote_Shortname, new_remote_repo).Output() + _, err = exec.Command("git", "remote", "add", npsetup.Remote_Label, new_remote_repo).Output() ifFerr("Unable to set git remote for new repo", err) fmt.Println("Added remote repo to git as origin") @@ -247,6 +252,19 @@ func main() { fmt.Println("Pushed initial commit to remote") } + + if npsetup.Setup_gitea { + // create gitea repo + grcomm := fmt.Sprintf("%s:%s/%s.git", npsetup.Gitea_ssh_host_label, npsetup.Gitea_user_account, newProjectName) + _, err = exec.Command("git", "remote", "add", npsetup.Remote_Label_gitea, grcomm).Output() + ifFerr("Unable to set git remote for new project to gitea", err) + fmt.Println("Added remote repo to git for gitea as ", npsetup.Remote_Label_gitea) + + // -u sets upstream tracking + _, err = exec.Command("git", "push", "-u", npsetup.Remote_Label_gitea, "main").Output() + ifFerr("Unable to push new project to gitea", err) + fmt.Println("Pushed new project to gitea") + } } fmt.Printf("New %s Project: %s ready\n", projType, newProjectName)