Skip to content

Commit

Permalink
Merge pull request #29 from Shyam-Sundar-Raju/main
Browse files Browse the repository at this point in the history
Modified folder structure and added unit test for issue#12
  • Loading branch information
Ashrockzzz2003 authored Feb 23, 2025
2 parents 704d5c7 + 64f80c3 commit 6d9e357
Show file tree
Hide file tree
Showing 25 changed files with 1,001 additions and 300 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CI (Go Unit Tests)

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install dependencies
run: go mod download

- name: Run unit tests
run: go test ./... -v

- name: Go Lint Test
uses: golangci/golangci-lint-action@v6.5.0
36 changes: 36 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"fmt"
"os"

"amrita_pyq/cmd/internal/configs"
"amrita_pyq/cmd/internal/logo"
"amrita_pyq/cmd/internal/menu"
"amrita_pyq/cmd/internal/requestclient"
"amrita_pyq/cmd/internal/webclient"

"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "ampyq",
Short: "Amrita PYQ CLI",
Long: `A CLI application to access Amrita Repository for previous year question papers.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(configs.LogoStyle.Render(logo.LOGO_ASCII))
cs := menu.CourseSelect{
ReqClient: requestclient.RequestClient{
WebClient: webclient.DefaultWebClient{},
},
}
cs.ChooseCourse()
},
}

func Execute() {
if err := Cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
27 changes: 27 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"github.com/spf13/cobra"
"testing"
)

func TestRoot(t *testing.T) {

//Test RootCmd
if _, ok := interface{}(Cmd).(*cobra.Command); !ok {
t.Errorf("RootCmd is not of type *cobra.Command")
}

if Cmd.Use != "ampyq" {
t.Errorf("Expected Use: 'ampyq', Got: %v", Cmd.Use)
}

if Cmd.Short != "Amrita PYQ CLI" {
t.Errorf("Expected Short: 'Amrita PYQ CLI', Got: %v", Cmd.Short)
}

long := `A CLI application to access Amrita Repository for previous year question papers.`
if Cmd.Long != long {
t.Errorf("Expected Long: %q, Got: %v", long, Cmd.Long)
}
}
7 changes: 0 additions & 7 deletions cmd/configs.go

This file was deleted.

39 changes: 0 additions & 39 deletions cmd/helpers.go

This file was deleted.

34 changes: 34 additions & 0 deletions cmd/internal/configs/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package configs

import (
"github.com/charmbracelet/lipgloss"
)

const (
BASE_URL = "http://dspace.amritanet.edu:8080"
COURSE_URL = BASE_URL + "/xmlui/handle/123456789/"
COURSE_LIST_URL = COURSE_URL + "16"
)

// Styling for the logo and error messages.
var (
LogoStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#01FAC6")).
Bold(true)

ErrorStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("9")).
Bold(true).
Underline(true).
Padding(0, 1).
Margin(1, 0, 1, 0).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("1"))

FetchStatusStyle = lipgloss.NewStyle().
PaddingLeft(2).
Foreground(lipgloss.Color("6")).
Bold(true).
Margin(1, 0)
SelectionHistory []string
)
38 changes: 38 additions & 0 deletions cmd/internal/configs/configs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package configs

import (
"testing"
)

func TestConfigs(t *testing.T) {
tests := []struct {
name string
url string
wantUrl string
}{
{
name: "SuccessBaseURLDeclaredProperly",
url: BASE_URL,
wantUrl: "http://dspace.amritanet.edu:8080",
},
{
name: "SuccessCourseURLDeclaredProperly",
url: COURSE_URL,
wantUrl: "http://dspace.amritanet.edu:8080/xmlui/handle/123456789/",
},
{
name: "SuccessCourseListURLDeclaredProperly",
url: COURSE_LIST_URL,
wantUrl: "http://dspace.amritanet.edu:8080/xmlui/handle/123456789/16",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if tc.url != tc.wantUrl {
t.Errorf("Expected %s, got %s", tc.wantUrl, tc.url)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/logo.go → cmd/internal/logo/logo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package logo

const LOGO_ASCII string = `
__ __ _____ _____ _______ _______ ______
Expand Down
24 changes: 24 additions & 0 deletions cmd/internal/logo/logo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package logo

import (
"testing"
)

const wantLogo = `
__ __ _____ _____ _______ _______ ______
/\ | \/ | __ \|_ _|__ __|/\ | __ \ \ / / __ \
/ \ | \ / | |__) | | | | | / \ | |__) \ \_/ / | | |
/ /\ \ | |\/| | _ / | | | | / /\ \ | ___/ \ /| | | |
/ ____ \| | | | | \ \ _| |_ | |/ ____ \ | | | | | |__| |
/_/ \_\_| |_|_| \_\_____| |_/_/ \_\ |_| |_| \___\_\
`

func TestLogo(t *testing.T) {
t.Run("TestLogoASCII", func(t *testing.T) {
t.Parallel()
if LOGO_ASCII != wantLogo {
t.Errorf("Expected %v, Received %v", wantLogo, LOGO_ASCII)
}
})
}
89 changes: 89 additions & 0 deletions cmd/internal/menu/coursechoose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package menu

import (
"fmt"
"os"
"time"

"amrita_pyq/cmd/internal/requestclient"
"amrita_pyq/cmd/util"

"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/spinner"

"amrita_pyq/cmd/internal/configs"
)

type (
Subject struct {
name string
path string
}

CourseSelect struct {
ReqClient requestclient.RequestClient
}
)

func (cs *CourseSelect) ChooseCourse() {
action := func() {
time.Sleep(2 * time.Second)
}
if err := spinner.New().Title("Fetching Courses").Action(action).Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}

resources, err := cs.ReqClient.GetCoursesReq(configs.COURSE_LIST_URL)
if err != nil {
fmt.Println(configs.ErrorStyle.Render(fmt.Sprintf("Error: %v\n", err)))
os.Exit(1)
}

var selectedOption string
var subjects []Subject
var options []huh.Option[string]

for _, res := range resources {
subject := Subject{res.Name, res.Path}
subjects = append(subjects, subject)
options = append(options, huh.NewOption(subject.name, subject.name))
}
options = append(options, huh.NewOption("Quit", "Quit"))

// First menu does NOT display history yet
form := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title("Available Courses").
Options(options...).
Value(&selectedOption),
),
)

err = form.Run()
if err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}

// Auto-exit if "Quit" is selected
if selectedOption == "Quit" {
util.QuitWithSpinner()
}

// Store only the selected course
configs.SelectionHistory = []string{selectedOption} // Reset history to show only last selected

// Move to the next menu (Second Menu)
for _, subject := range subjects {
if subject.name == selectedOption {
url := configs.BASE_URL + subject.path
semTable := SemTable{
ReqClient: cs.ReqClient,
}
semTable.ChooseQuestionSetFromSemester(url)
break
}
}
}
Loading

0 comments on commit 6d9e357

Please sign in to comment.