Skip to content
Snippets Groups Projects
Commit 0a4eba85 authored by Nathan Bockisch's avatar Nathan Bockisch
Browse files

WS-702: Added CLI for modifying qa_staff table

parent 03c56646
No related branches found
No related tags found
1 merge request!902WS-702: Added CLI for modifying qa_staff table
Pipeline #4928 passed
module ssa/mod_analyst
go 1.18
require (
github.com/lib/pq v1.10.5 // indirect
github.com/magiconair/properties v1.8.6 // indirect
)
github.com/lib/pq v1.10.5 h1:J+gdV2cUmX7ZqL2B0lFcW0m+egaHC2V3lpO8nWxyYiQ=
github.com/lib/pq v1.10.5/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
package main
import (
"flag"
"os"
"ssa/mod_analyst/pkg/db"
)
func main() {
// user args
var user_info db.UserInfo
var is_remove bool
// DB connection args
var db_info db.DbInfo
// Get user properties
flag.BoolVar(&user_info.Is_aod, "aod", false, "When adding a user, makes them part of the AOD group (by default they are added to the DA group)")
flag.StringVar(&user_info.Name, "name", "", "The full name of the DA/AOD to be added, use quotes for the first and last name (e.g. -name 'First Last')")
flag.StringVar(&user_info.Email, "email", "", "The email of the DA/AOD to be added")
flag.BoolVar(&is_remove, "rm", false, "Remove the user from the database")
// Get DB connection args
flag.BoolVar(&db_info.Is_ssl, "ssl", false, "Use ssl for the database connection")
flag.IntVar(&db_info.Port, "port", 5432, "The port for the database")
flag.StringVar(&db_info.Dbname, "dbname", "archive", "The name of the database to connect to")
flag.StringVar(&db_info.Host, "host", os.Getenv("HOSTNAME"), "The host where the database is located")
flag.StringVar(&db_info.Property_path, "prop", "/home/casa/capo/${CAPO_PROFILE}.properties", "Path to the properties file with the login details for the database")
flag.Parse()
// Make sure name is given
if len(user_info.Name) == 0 {
flag.Usage()
return
}
if is_remove {
db.RemoveUser(user_info, db_info)
} else {
db.AddUser(user_info, db_info)
}
}
package db
import (
"github.com/magiconair/properties"
)
type DbInfo struct {
Host string
Port int
User string
Password string
Dbname string
Property_path string
Is_ssl bool
}
/**
* Pull the login information for the database from a properties file
*
* @param db_info a DbInfo type with information to connect to the database
* @return a DbInfo type holding db_info's data with the User and Password
* fields populated
**/
func getDbLoginFromProperties(db_info DbInfo) DbInfo {
prop := properties.MustLoadFile(db_info.Property_path, properties.UTF8)
db_info.User = prop.GetString("metadataDatabase.jdbcUsername", "archive")
db_info.Password = prop.GetString("metadataDatabase.jdbcPassword", "docker")
return db_info
}
package db
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
type UserInfo struct {
Name string
Email string
Is_aod bool
}
/**
* Check if an error happened and panic on it if so
*
* @param err An error object to report
**/
func checkError(err error) {
if err != nil {
panic(err)
}
}
/**
* Establish a connection to the database
*
* @param db_info A DbInfo type with information to connect to the database
* @return A DB object with an open connection to the database
**/
func getConnection(db_info DbInfo) *sql.DB {
// Get db info and build string to get connection
db_info = getDbLoginFromProperties(db_info)
ssl_mode := "disable"
if db_info.Is_ssl {
ssl_mode = "require"
}
conn_info := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", db_info.Host, db_info.Port, db_info.User, db_info.Password, db_info.Dbname, ssl_mode)
db, err := sql.Open("postgres", conn_info)
checkError(err)
err = db.Ping()
checkError(err)
return db
}
/**
* Add a user to the qa_staff table in the database
*
* @param name a string with the name of the person to be added
* @param is_aod a bool to check if the user is an AOD or not
* @param db_info a DbInfo type with information to connect to the database
**/
func AddUser(user_info UserInfo, db_info DbInfo) {
// Get a connection to the database
db := getConnection(db_info)
defer db.Close()
group := "DA"
if user_info.Is_aod {
group = "AOD"
}
// Add the user
insertStatement := `insert into "qa_staff"("user_name", "group", "available", "email") values($1, $2, $3, $4)`
_, err := db.Exec(insertStatement, user_info.Name, group, true, user_info.Email)
checkError(err)
fmt.Println("Added " + user_info.Name + " to qa_staff")
}
/**
* Remove a user from the qa_staff table in the database
*
* @param name a string with the name of the person to be removed
* @param db_info a DbInfo type with information to connect to the database
**/
func RemoveUser(user_info UserInfo, db_info DbInfo) {
// Get a connection to the database
db := getConnection(db_info)
defer db.Close()
// Remove the user
deleteStatement := `delete from "qa_staff" where "user_name"=$1`
_, err := db.Exec(deleteStatement, user_info.Name)
checkError(err)
fmt.Println("Removed " + user_info.Name + " from qa_staff")
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment